static variables in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Hello all, In this article we are going to see the behavior of static variables and how are they different from non static variables. We will first see some key points about them.

  • static variables are bound to a class and not the objects of the class
  • non-static variables are of a particular object.
  • static variables are shared across all the objects of the class.
  • All objects will have their own copy of non-static variables

Let us directly take a program into consideration.

  • Create class StaticNonStaticDemo
  • create a static variable called staticVar and non-static variable called nonStaticVar
  • Create Constructor and increment both variables in it.
  • Create a main method.

This gives the output as follows.

Lets see what happens here step by step.

  • When class class is loaded, staticVar is initialized to 0 because it is static.
  • nonStaticVar will be initialized to 0 when first object of the class is created
  • StaticNonStaticDemo obj1 = new StaticNonStaticDemo();invokes the constructor and the constructor increments staticVar and nonStaticVar .
  • After this object creation,  staticVar = 1  and  nonStaticVar = 1  because of the increment done in constructor
  • Now after the second object creation, i.e StaticNonStaticDemo obj2 = new StaticNonStaticDemo(); , when we print staticVar and nonStaticVar then  staticVar=2  and nonStaticVar = 1.

Now why this happens differently for static variables and non static variables? As per the points mentioned about, static variables are shared variables for all the instances of the class while all the instances will have a copy of non static variables for each instance. This diagram represents the scenario.

shared static variables

shared static variables

As we have incremented nonStaticVar in the constructor, the value of each nonStaticVar for each object will be 1. If you do  obj2.setNonStaticVar(10);  then  obj3.getNonStaticVar()  will still return 1 because obj2 and obj3 will have separate copies of nonStaticVar for them.

Now in case of static variable, if you do  obj2.setStaticVar(10);  then  obj3.getStaticVar()  will return value 10 because static variables are shared across all the instances of the class.

The important thing we should remember about this is. Even if you say  obj2.setStaticVar(10);  implicitly it is converted into StaticNonStaticDemo.setStaticVar(10) . and  obj3.getStaticVar();  is treated as  StaticNonStaticDemo.getStaticVar();  i.e. static variables are always accessed by its class name. Never access static variables using object references. It causes unnecessary confusion.

I hope this article helps understand the concept of non-static and static variables in java.

 

 

 

Share Button

5 comments for “static variables in java

  1. Sudhir Kumar
    November 22, 2013 at 5:53 am

    ooohhk superb, I am studying this Topic from many year and currently from 1 year i have studied all of java/j2ee, struts, spring ,hibernate, xml. sometime it was confusing. I got it .

  2. Sudhir Kumar
    November 22, 2013 at 5:55 am

    can you explain about static bloks

    • Prasad Kharkar
      November 22, 2013 at 10:50 am

      Hi Sudhir, Read this.

  3. Pranaya
    June 27, 2016 at 1:46 am

    Though static variable can be accessed by objects, but why to access static variable by objects?

    • June 27, 2016 at 7:39 am

      HI Pranaya, as mentioned in article above, “we can access static variable using reference variables, but it is advised never to use them as it causes confusion”.

Leave a Reply

Your email address will not be published. Required fields are marked *