Search This Blog

Friday, May 9, 2014

How STATIC keyword works in Java?

Dear Friends , As you all would be aware of static variables, static methods , static classes and static inner classes. So have you ever thought about the pros and cons of static use in java . Lets have a look on the explanation to get a deeper idea about its uses.


Static Variables 

  • Static variable are known as class level variable also. So it will be accessed on class only whenever you want to search from outside of this class.
  • Some programmers use object to access on static variables but that's a programmer fault, Its recommended to access static variables only on class level.
  • Static variables always share a common memory location for all the objects which access them.
  • Static variables can be accessed inside non static methods as well but its not true for non static variables in java. It means non static variables can be accessed only in non static methods.
  • For each object of a class , non static variables will have the different references however static variable will have the same reference for all the objects. for example-
          private static int a = 10;//static declaration of java variable
          private int b = 20;//non static java variable
  • Static variables are bonded using static binding at compile time so they are comparatively faster than there non-static counter part which were bonded during run time.
  • Static fields are initialized at the time of class loading in Java, opposite to instance variable which is initialized when you create instance of a particular class.

Static Methods

  • Static methods in java have the same loading mechanism as static variables.
  • These methods can access or use only static variables , non static variables are not allowed inside static methods.
  • static method can not be override into its subsequent child classes because they are part of the class in which they have been declared not a part of its object. so if you have same static  method in subclass and super class , method will be invoked based on declared type of object instead of runtime.
  • Be careful while using static keyword in multi-threading or concurrent programming because most of the issue arise of concurrently modifying a static variable by different threads resulting in working with stale or incorrect value if not properly synchronized. most common issue is race condition which occurs due to poor synchronization or no synchronization of static variable.
  • Java static methods cannot use the ‘this’ keyword

Static Class

  • The classes which contains only static methods are known as Static class. 
  • Normally we make a class static in Java when we want a single resource to be shared between all instances and normally we do this for utility classes which are required by all components and which itself doesn't have any state. Sometime interviewer ask  when to use Singleton vs Static Class in Java for those purpose,answer is that if its completely stateless and it work on provided data then you can go for static class otherwise Singleton pattern is a better choice.

That's all on What is static variable, method and nested static class in Java. knowledge of static keyword in Java is must for any Java programmer and skill to find out when to use static variable or static method is an important skill. Incorrect and careless use of static variable and static method in Java will result in serious concurrency issues like deadlock and race condition in Java.


No comments :

Post a Comment