Static Instance Variable And Non-Static Instance Variable Difference - Java

Introduction

 
Static is the keyword in Java which can be used with variables, methods and blocks. However, the meaning of the static keyword differs with the place it is  being used.
 
In this article, I will explain the use of the static keyword with the variable, by providing you with the difference between the static instance variable and the non-static instance variable.
 

Static Instance Variable vs Non-static Instance Variable

 
First.java
  1. public class First {  
  2.       
  3.     //static instance variable named "a"  
  4.     static int a=0;  
  5.       
  6.     //non-static instance variable named "b"  
  7.     int b=0;  
  8.       
  9.     void displayValue()  
  10.     {  
  11.         System.out.println("static variable     a   :   "+a);  
  12.         System.out.println("non-static variable b   :   "+b+"\n");  
  13.     }  
  14.       
  15.     void incrementValue()  
  16.     {  
  17.         a++; //incrementing static variable "a" by 1  
  18.         b++; //incrementing non-static variable "b" by 1  
  19.     }  
  20. }  
Second.java
  1. public class Second extends First {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         //Object creation of First Class  
  6.         First f1 = new First();  
  7.         First f2= new First();  
  8.   
  9.         //Object creation of Second Class  
  10.           
  11.         Second s1 = new Second();  
  12.         Second s2 = new Second();  
  13.           
  14.         System.out.println("Object f1");  
  15.         f1.incrementValue();  
  16.         f1.displayValue();  
  17.           
  18.         System.out.println("Object f1-Again");  
  19.         f1.incrementValue();  
  20.         f1.displayValue();  
  21.           
  22.         System.out.println("Object f2");  
  23.         f2.incrementValue();  
  24.         f2.displayValue();  
  25.           
  26.         System.out.println("Object f2-Again");  
  27.         f2.incrementValue();  
  28.         f2.displayValue();  
  29.           
  30.         System.out.println("Object s1");  
  31.         s1.incrementValue();  
  32.         s1.displayValue();  
  33.           
  34.         System.out.println("Object s2");  
  35.         s2.incrementValue();  
  36.         s2.displayValue();  
  37.     }  
  38.   
  39. }  
In this example, there are
  • Two classes, First and Second, where Second inherits First
  • One static variable a with value 0
  • One non-static variable b with value 0
  • incrementValue() method to increment a and b by 1
  • displayValue() method to display a and b value to the console
  • Two objects f1 and f2 for First Class
  • Two objects s1 and s2 for Second Class 
  1. Accessing two methods, incrementValue() and displayValue() by the First class object named "f1".

    static variable a : 1
    non-static variable b : 1
  2. Accessing two methods, incrementValue() and displayValue() by the First class object named "f1 Again".

    static variable a : 2
    non-static variable b : 2
  3. Accessing two methods, incrementValue() and displayValue() by the First class object named "f2".

    static variable a : 3
    non-static variable b : 1
  4. Accessing two methods, incrementValue() and displayValue() by the First class object named "f2 Again".

    static variable a : 4
    non-static variable b : 2
    From this, it is understood that the static variable is common to all the instances of the class and non-static variable is specific to the particular object.
  5. Accessing two methods, incrementValue() and displayValue() by the First class object named "s1".

    static variable a : 5
    non-static variable b : 1
  6. Accessing two methods, incrementValue() and displayValue() by the First class object named "s1".

    static variable a : 6
    non-static variable b : 1
From the above two steps, it is understood that the static variable is common to all the instances of the superclass and subclass.
 
Final output
 
 

Conclusion 

 
In this article, we learned that the static variable is shared by every instance of the classes. So, it can be used for memory management purposes. But the non-static variable is specific to the object created and it occupies more space.


Similar Articles