Java Static Variables

Summary: in this tutorial, you will learn how to use Java static variables to maintain shared data among objects of a class.

Introduction to the Java static variables

When you define a class with a field, it means that every instance of the class will possess its unique copy of the field. In other words, the field is bound to each individual instance of the class.

For example, the following defines a Person class that has a field name:

class Person {
    private String name;

    public Person(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}Code language: Java (java)

When you create two Person objects, each will have a separate copy of the name field:

public class App {
    public static void main(String[] args) {
        var p1 = new Person("John");
        var p2 = new Person("Jane");

        System.out.println(p1.getName()); // John
        System.out.println(p2.getName()); // Jane
    }
}Code language: Java (java)

In this example, the value of the name field of p1 is John while the value of the name field of p2 is Jane. In other words, the name field belongs to an instance of the Person class. That is why the name field is called an instance variable.

In Java, a static variable is a type of field that belongs to the class rather than an instance of the class.

It means that there is only one copy of a static variable shared among all instances of the class. In other words, static variables are bound to the class.

Static variables are also known as class variables or static fields.

Declaring static variables

To declare a static variable, you use the static keyword followed by the variable’s data type and name:

public class MyClass {
    static dataType variableName;
}Code language: Java (java)

Also, you can initialize the static variable when declaring it:

public class MyClass {
    static dataType variableName = value;
}Code language: Java (java)

For example, the following defines the count as the static variable of the Person class and increases its value by one in the constructor:

class Person {
    static int count = 0;
    private String name;

    public Person(String name) {
        this.name = name;
        count++;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}Code language: Java (java)

Accessing static variables

To access a static variable, you use the class name, followed by the dot operator and variable name:

className.variableName;Code language: Java (java)

For example, the following illustrates how to access the count static variable of the Person class:

Person.countCode language: Java (java)

Because the constructor of the Person class increases the value of the count static variable by 1, if you create two person objects, the value of the count will be 2:

public class App {
    public static void main(String[] args) {
        var p1 = new Person("John");
        var p2 = new Person("Jane");
        System.out.println("Person count:" + Person.count);
    }
}Code language: Java (java)

Output:

Person count:2Code language: Java (java)

Summary

  • Java static variables are shared among the instances of the class.
Was this tutorial helpful ?