Thursday 13 March 2014

What does "static" mean in Java?


I have gone through many books of java but not many give sufficient explanation of what is the meaning of the keyword "static" in Java and when does one use it.


STATIC VARIABLES:


Making a variable static basically means that only one copy of the variable is shared between all the objects of the class. If one object changes the value of that variable, all the variables get the updated value.

Consider the following code snippet:
class StateOfACountry; 
 public String stateName;
 public String CM_name;
 public static String PM_name;
  
 public static void main(String []args){
  StateOfACountry state1 = new StateOfACountry();
  state1.CM_name = "CM1";
  state1.PM_name = "PM";
  System.out.println("State 1 CM name " + state1.CM_name);
  System.out.println("State 1 PM name " + state1.PM_name);
 
  StateOfACountry state2 = new StateOfACountry();
  System.out.println("State 2 CM name " + state2.CM_name);
  System.out.println("State 2 PM name " + state2.PM_name);
  
  state2.CM_name = "CM2";
  state2.PM_name = "PMabc";
  
  System.out.println("State 2 CM name " + state2.CM_name);
  System.out.println("State 2 PM name " + state2.PM_name);

  System.out.println("State 1 CM name " + state1.CM_name);
  System.out.println("State 1 PM name " + state1.PM_name);
  }
}
The output is:
State 1 CM name CM1
State 1 PM name PM
State 2 CM name null
State 2 PM name PM
State 2 CM name CM2
State 2 PM name PMabc
State 1 CM name CM1
State 1 PM name PMabc
In the above example, the variables stateName, CM_name are not static while the variable PM_Name is. This is correct logically because if there are many states in a country, the Chief Minister (CM) of one state may be different from another. However, all states will share the same Prime Minister (PM) name. Thus we see that, if there is a variable which is common amongst all the objects in the same class, we should treat it as static.

Now, consider a case of re-elections. If the CM of state1 is changed, it is not necessary that the CM of state2 be changed as well. However, if the PM of the country is changed, then the change is reflected on the PM of all the states! (which is correct logically).
Thus, we see that the static variables belongs to the class on a whole and not to a particular object of the class. In the above case, a Prime Minister (PM) belongs to a country on a whole, rather than the state of a country.
All such variables which are not tied to an object should be made static in your programs.
STATIC FUNCTIONS:
The definition of static extends to functions as well i.e. a copy of static function is shared between all the objects of the class. A function is made static when two static functions have nothing in common between them.
Also, there is an additional clause: Static functions can access and operate on only static variables. This, of course, is logical as static functions are something that belong to the class rather than an object, and therefore should operate only on variables that too belong to the class.
Accessing non-static variables in static functions causes an error (try it!)
Consider the above example, now modified:
class StateOfACountry{
   public String stateName;
   public String CM_name;
   public static String PM_name;
           
   public static void changePM(String newPM_name){
     PM_name = newPM_name;
   }
           
   public void changeCM(String newCM_name){
     this.CM_name = newCM_name;
   }
 
   public static void main(String []args){
 
      StateOfACountry state1 = new StateOfACountry();
      state1.CM_name = "CM1";
      state1.PM_name = "PM";
           
      StateOfACountry state2 = new StateOfACountry();     
      state2.CM_name = "CM2";
      state2.PM_name = "PMabc";
      state1.changeCM("newCM1");
      System.out.println(state1.CM_name);
      System.out.println(state2.CM_name);
           
      state1.changePM("newPM1");
      System.out.println(state1.PM_name);
      System.out.println(state2.PM_name);          
     }
}

The output is:

newCM1

CM2

newPM1

newPM1


Thus we see that the function changeCM operates on the CM_name of a particular object, while the static function changePM operates on the static variable PM_name which is common for all the objects in the class.


INITIALIZING A STATIC VARIABLE:


Since static variables are not tied to any objects, they need not be initialized using an object. In other words, they do not have an “object state”.

public class MeaningOfStatic {
public static int var = display();
  public MeaningOfStatic() {
     System.out.println("Constructor Entered. Object is instantiated.");
  }
 public static int display(){
     System.out.println("Hello World");
     System.out.println(var);
     return 10;
   }
 
  public static void main(String[] args) {
     System.out.println("Main entered.");
     MeaningOfStatic object = new MeaningOfStatic();
     object.display();
     System.out.println(Math.PI);
     System.out.println(Math.sqrt(2)); //No object of Math is                                                initialized to invoke sqrt, because its static! 
     }
}

The output is:

Hello World

0
Main entered.
Constructor Entered. Object is instantiated.
Hello World
10
Thus we see that the static variable var was declared even before an object was initialized. It was also assigned value using the static function even before the main function was started! Because it isn’t tied to any particular object.
All lang.Math methods in java are static (e.g. Math.Random(), Math.sqrt()) because it wouldn’t make sense to tie all this methods to an object i.e. the methods Math.random, Math.sqrt have nothing in common.

No comments:

Post a Comment