Class (static) variables

  • Only instance variable can be marked with static keyword.
  • Syntax: static variable declaration
  • static data_type variableName;
    Where, <> is optional.

  • Parameter and local variable cannot be marked with static variable.
  • There is only one copy of the static variable (also known as global variable) and all instance of the class share the static variable.
  • Static variable are stored in one fixed location in memory and any object can change the value of a class variable.
  • Static variable can be accessed without creating an instance of the class.
  • Also called as static fields, class variables, and static variables.

Suppose, you wanted to keep track of number of students created and assign a id value which is incremented automatically whenever a new student is created.

The following code explains the above scenario.

package com.ibytecode.keywords.staticdemo;
public class Student {

	private String name;
	int id;
	static int count; //Initialized to zero
		
	public Student(String name) {
		this.name = name;
		id = ++count;
	}

	@Override
	public String toString() {
		return "Student [Id=" + id + ", Name=" + name + "]";
	}

	public static void main(String[] args) {
		Student s1 = new Student("John");
		Student s2 = new Student("Kumar");
		Student s3 = new Student("Ram");
		
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		
		System.out.println("Number of Students created: " + count);
	}
}

Student [Id=1, Name=John] Student [Id=2, Name=Kumar] Student [Id=3, Name=Ram] Number of Students created: 3

How this works?

  • Static variable, count, is initialized to zero when the class is first loaded by the JVM. You can also manually initialize the static variable
  • static int count = 0;
  • When the student object is created in the main() method, the constructor of the class runs and increments the count variable and assigns the value to the id.

In the above code, lets see what happens if we change the count variable as instance variable

package com.ibytecode.keywords.staticdemo;
public class Student {
	private String name;
	int id;
	int count; //Instance Variable
		
	public Student(String name) {
		this.name = name;
		id = ++count;
	}
	public static void main(String[] args) {
		Student s1 = new Student("John");
		Student s2 = new Student("Kumar");
		Student s3 = new Student("Ram");
		
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);		
		System.out.println("Number of Students created: " + count);
	}
}

We will get the following compile time error,

Cannot make a static reference to the non-static field count
at com.ibytecode.keywords.staticdemo.Student.main(Student.java:19)

You cannot access a non-static (instance) variable inside a static method. The JVM does not know which Student instance’s count you are trying to access. You have to use objectName.variable (s1.count) if you want to access the instance variable inside the static method.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.