Static VS Non-Static Class Members

Static vs non-static class members

You need to think about static variables as belonging to the class, not to instances of the class.

If, in all instances of the class this variable should be identical, use a static variable.

If not, use an instance variable.

In general having public static variables is bad practice - it is a shared global resource and if you change it you need to synchronize access to it. Having global state is something you want to avoid as much as possible.

Difference between static and non static members?

The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed.

In OOP, static variables are used for values which cannot be stored by an instance variable. static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class we are trying to refer.

e.g. Supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

References:

  1. Static vs. Non-Static method in C#
  2. Static vs. non-static method

What is the difference between a static method and a non-static method?

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

Example:

class Foo {
int i;

public Foo(int i) {
this.i = i;
}

public static String method1() {
return "An example string that doesn't depend on i (an instance variable)";
}

public int method2() {
return this.i + 1; // Depends on i
}
}

You can call static methods like this: Foo.method1(). If you try that with method2, it will fail. But this will work: Foo bar = new Foo(1); bar.method2();

c# What is the different between static class and non-static (I am talking about the class itself not the field)

If you look at the IL code, the static class will be abstract and sealed which gives two important qualities:

  • You cannot create instances from it
  • It cannot be inherited

A consequence of the first point is that a static class cannot contain non-static members. There may be many uses of static members in a non-static class. One common use is to have a class factory:

public class SomeClass
{
public int SomeInt { get; set; }

public static SomeClass Create(int defaultValue)
{
SomeClass result = new SomeClass();
result.SomeInt = defaultValue;
return result;
}
}

What is the difference between static methods in a Non static class and static methods in a static class?

The only difference is that static methods in a nonstatic class cannot be extension methods.


In other words, this is invalid:

class Test
{
static void getCount(this ICollection<int> collection)
{ return collection.Count; }
}

whereas this is valid:

static class Test
{
static void getCount(this ICollection<int> collection)
{ return collection.Count; }
}

Java static and non-static variables in the same class

You are correct in your assertion ... sort-of. Statics aren't really shared between instances of the class because you don't need a class instance to reference one.

public class Foo {
public static String MYSTRING = "test";
}

can be accessed without an instance of Foo

String localString = Foo.MYSTRING;

I wouldn't really worry much about how the JVM chooses to store the memory references for the static variables, that is an implementation detail that is delegated to the JVM designer.

There aren't any pitfalls to having a class that defines both static and non-static (instance) variables, it happens all the time and is perfectly natural ... as long as you understand how statics work, which it seems that you do.

Java - about using classes with static members vs. not

The semantics of static vs. non-static member variables and methods are completely different. Non-static variables are members of instances of the class; each instance has its own copy. Static variables are members of the class itself; they're not tied to any particular instance.

Similarly, non-static methods operate on instances of the class, static methods aren't tied to a particular instance.

You should use static/non-static as the problem requires. This isn't a question of best practices.

static vs non static

It might be thin but there is very clear distinction. You declare a field as static when it is not at all related to any instance of a class.

A simple usecase of static field is declaring constants using final keyword, e.g.:

public static final int MAX_ALLOWED = 10;

Same is the case with methods also. You declare a method as static when it is not dependent on instance of a class OR the state of the class. That's the reason a static method cannot use instance members of a class.



Related Topics



Leave a reply



Submit