Showing posts with label static versus singleton. Show all posts
Showing posts with label static versus singleton. Show all posts

Monday, August 5

When to use static members in java

                               When to use statics 

Statics should be used when you don't want to have varying behavior for different objects. 

Statics should be used when data is not instance dependent and for all existing instances of static member you want to apply same state.

Statics should not be used at all If they are not actually required as they create dependencies and references to other classes and class loader in JVM and stay in memory for long time. So they are not considered for garbage collection once they are done with current work Instead they are de-referenced with their loading classes as they maintain a reference to static classes

If you need to access the data only in few instances then it is better to create stateful instance at runtime Instead of choosing static design . So whether to use static or not is very cautious design decision .

Static declaration is kind of situation similar to singleton design pattern where you don't want  lot of different instances for different client calls instead it is same single instance that caters to call. 

Static should be used for the methods where you have mostly utility methods in the class and we hardly need to maintain any object state to achieve the required action. Math class is full of static methods .All methods in class are utility methods and It is very logical to declare them static. 

Should a method of class be invoked before it's any of the available constructor is called? If yes, is the answer because it hardly matters for our functionality If constructor is invoked or not , then it's good to use static method . But again one need to know how much is the volume of client programs who would actually use that . 

So summarizing a bit static methods can be used when : 

1 for utility classes that will remain as it is .
2.Methods being declared static have no dependency on instance data
3.Some data required to be shared among multiple instances SO instead of creating them every-time , create once and share among all .
4. Static methods can not be overridden . So keep in mind So can't apply more specific behavior on static method with overriding technique.

While writing unit test cases you can not mock up the static data differently for a specific instance And you are sharing the same data across multiple instances. 

One argument is We can also use singleton instead of static class . But should we do that or not can be very good design consideration. Static members resides at perm Gen in memory space while singletons  resides on heap . So statics stay in memory till the containing program is not unloaded while singletons gets garbage collected regularly So that could give good preference to singleton wherever possible instead of using statics.

One good rule of java we need to keep in mind is that static method can not invoke non-static method directly . It is must for them to create instance of class enclosing non-static member and then invoke method or access variable using that . So that should be another part of our thought process while designing static classes

Also analyze how static method will behave differently in case of multi-threaded environment and code Synchronization cases. Static method will take lock on class to block the access to all synchronized static methods of the class.

Static modifier is preferred to declare constant variables in a class As they never depend on instances instead are declared once as final values and used forever everywhere.