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
- public class First {
-
-
- static int a=0;
-
-
- int b=0;
-
- void displayValue()
- {
- System.out.println("static variable a : "+a);
- System.out.println("non-static variable b : "+b+"\n");
- }
-
- void incrementValue()
- {
- a++;
- b++;
- }
- }
Second.java
- public class Second extends First {
-
- public static void main(String[] args) {
-
-
- First f1 = new First();
- First f2= new First();
-
-
-
- Second s1 = new Second();
- Second s2 = new Second();
-
- System.out.println("Object f1");
- f1.incrementValue();
- f1.displayValue();
-
- System.out.println("Object f1-Again");
- f1.incrementValue();
- f1.displayValue();
-
- System.out.println("Object f2");
- f2.incrementValue();
- f2.displayValue();
-
- System.out.println("Object f2-Again");
- f2.incrementValue();
- f2.displayValue();
-
- System.out.println("Object s1");
- s1.incrementValue();
- s1.displayValue();
-
- System.out.println("Object s2");
- s2.incrementValue();
- s2.displayValue();
- }
-
- }
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
- Accessing two methods, incrementValue() and displayValue() by the First class object named "f1".
static variable a : 1
non-static variable b : 1
- Accessing two methods, incrementValue() and displayValue() by the First class object named "f1 Again".
static variable a : 2
non-static variable b : 2
- Accessing two methods, incrementValue() and displayValue() by the First class object named "f2".
static variable a : 3
non-static variable b : 1
- 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.
- Accessing two methods, incrementValue() and displayValue() by the First class object named "s1".
static variable a : 5
non-static variable b : 1
- 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.