Explain Java static variable with simple program example

Answer:  Java static variable is a variable that is preceded by static keyword and is shared by all the objects of a class. Declaration of an static variable is like below example

private static int customerObjectCount = 0;

Below class shows how to declare static and non-static variable in Java.

public class Customer {

	//Static variable
	private static int customerObjectCount = 0;
	//Non Static Variable
	private int price = 0;	
}

Static variable in Java has only one copy in the memory that is shared by all objects of a class. For example, if we create objects of the Customer class e.g. C1 and C2, both object will point to same static variable. Whereas, for non-static variable, both C1 and C2 object will have their own copy.

So, if object C1 changes its non-static variable, it will not be seen by object C2 and vice versa. But, if a static variable is changed by any object, all the object C1 and C2 will see the changed one.

Static variable program example in Java

Let’s have a simple example of customer class object counter in Java. Program will count the number of objects created in main program.

We will make the customerObjectCount variable as a static to give the correct object count. If we don’t make it static then output will be incorrect.

public class Customer {

	//Static variable
	private static int customerObjectCount = 0;
	
		
	public Customer(String name){
		//Print customer name
		
		System.out.println("Customer :"+name);
		//Increase the customer count
		++customerObjectCount;
		
		System.out.println("Customer object Count so for :"+ customerObjectCount);
	}		
	
	public void displayTotalCustomerCount(){
		
		System.out.println("Total Customer Count :"+ customerObjectCount);
	
	}
}

In below java test program, we will create 3 objects of the class Customer and all the objects will  call the constructor of the class and constructor will increment the value of customerObjectCount . Since, customerObjectCount  variable is static, as a result, all the objects  c1,c2 and c3 will see the updated value of it. Hence, result will be 3.

Also, note that using any object of the class, if we call displayTotalCustomerCount() function, they will give the same result, that is 3, because, all objects have updated value of customerObjectCount .

public class Test {

	public static void main(String[] args) {

		Customer c1 = new Customer("Jhon");
		Customer c2 = new Customer("Henry");
		Customer c3 = new Customer("Winston");	
		
		c1.displayTotalCustomerCount();
		c2.displayTotalCustomerCount();
		c3.displayTotalCustomerCount();
	}

}

Output:

Customer object Count so for :2
Customer :Winston
Customer object Count so for :3
Total Customer Count :3
Total Customer Count :3
Total Customer Count :3

What if above class contains non static variable instead of static?

From the Customer class, if we remove static keyword from variable customerObjectCount and make it not static e.g. int customerObjectCount = 0;, We will get unexpected output. Because, in this case, every object is having its own copy of the variable customerObjectCount. As a result, an object can not see the updated value by another object. Hence, value will be always 1,   resulting incorrect object counts.

Below is the output if we remove static keyword form customerObjectCount variable.

Output:

Customer object Count so for :1
Customer :Winston
Customer object Count so for :1
Total Customer Count :1
Total Customer Count :1
Total Customer Count :1

NOTES:

  • Non static variables are called instance/object variable, in opposite of that static variables are called class variable. Because, static variables are not part of an object of a class.
  • We can initialized Java static variable only once.
  • A java static variable can be accessed by only class name ( myclass.staticVariable), if it is public.

You may read about java static method explained.

Related Posts