Computer >> Computer tutorials >  >> Programming >> Java

Differences between static and non-static variables in Java


A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Static Variable

A static variable is also called a class variable and is common across the objects of the class and this variable can be accessed using class name as well.

Non-Static Variable

Any variable of a class which is not static is called non-static variable or an instance variable.

Following are the important differences between static and non-static variable.

Sr. No.
Key
Static
Non-Static
1
Access
A static variable can be accessed by static members as well as non-static member functions.
A non-static variable can not be accessed by static member functions.
2
Sharing
A static variable acts as a global variable and is shared among all the objects of the class.
A non-static variables are specific to instance object in which they are created.
3
Memory allocation
Static variables occupies less space and memory allocation happens once.
A non-static variable may occupy more space. Memory allocation may happen at run time.
4
Keyword
A static variable is declared using static keyword.
A normal variable is not required to have any special keyword.

Example of static vs non-static variable

JavaTester.java

public class JavaTester {
   public int counter = 0;
   public static int staticCounter = 0;
   public JavaTester(){
      counter++;
      staticCounter++;
   }
   public static void main(String args[]) {
      JavaTester tester = new JavaTester();
      JavaTester tester1 = new JavaTester();
      JavaTester tester2 = new JavaTester();
      System.out.println("Counter: " + tester2.counter);
      System.out.println("Static Counter: " + tester2.staticCounter);
   }
}

Output

Counter: 1
Static Counter: 3