Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static vs non static

Until few weeks back, I thought I understand when to make fields and methods static or non-static. For example, when a field (say object of another class) is unique for any number of objects for the class, it should be made static.

But then I read about JVM garbage collection a few weeks back.

I understand that static fields are never garbage collected and remain in memory all along, unless the class loader itself is garbage collected.

But if I don't make that field static, at least it will be garbage collected.

So, it seems there is a very thin line between making fields/methods static or not.

Can anybody please explain to me this thin line in deciding, so that my application is way more efficient.

like image 543
codingenious Avatar asked Dec 15 '13 08:12

codingenious


People also ask

What is static and non static in oops?

In the static method, the method use compile-time or early binding. For this reason, we can access the static method without creating an instance. In a non-static method, the method use runtime or dynamic binding. So that we cannot access a non-static method without creating an instance.

What means non static?

nonstatic in British English (ˌnɒnˈstætɪk ) adjective. computing. (in computer languages) not static.

What is static example?

An example of static is rubbing a balloon on one's hair and then have the balloon stick to a wall. adjective. 1. (electricity) Of, relating to, or producing stationary charges; electrostatic. adjective.

What is a non static variable?

Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.


2 Answers

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.

like image 64
anubhava Avatar answered Oct 10 '22 00:10

anubhava


These are not mandatory rules, but it is better to follow them:

When to use static fields:

  1. Constants Best usage of static fields are constants, which shall be final too.
  2. ThreadLocals Most of the time you define thread-local variables static, otherwise you usually lose the reference to them. But don't forget to release their contents when there is no more use (or you will encounter memory leaks). Most of the time these variables are final too.
  3. And very rare cases when you shall keep a reference to a (semantically) singleton object which could be accessed from many places. (For example a reference to Hibernate SessionFactory in HibernateUtils).

Remember that, static fields shall usually be immutable during runtime of your application (they are sometimes modified during start-up and tear-down of the application).

When to use static methods:

  1. Helper utility methods that's the normal case, when a method does not depend on state of its containing object. These methods can be useful, and can be accessed without instantiating any object.
  2. Factory methods Factory methods are another good example of where to use static methods.
  3. Private methods which are not depend on objects state, when a private method just have no dependency on object's state, it is much better to define it static to gain a little performance.
  4. void main(String[]) method shall be static too.
like image 44
Amir Pashazadeh Avatar answered Oct 09 '22 23:10

Amir Pashazadeh