Search Logic Blocks

Tuesday, January 28, 2020

Java: Static Variables

In last post Classes and Objects, we discussed how to create an instance of class and how to access its variables and methods. But sometimes, you need a variable (data field) that is common to all the objects created. To achieve that, the static keyword is used to declare the variable. The variables declared as static are called static variables. Static variables are allocated memory only once and retain their values.

Class Student (Student.java)
public class Student {
    String name;
    int age;
    static int total = 0;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
        total++;
    }
}
In above program, we have created a class named as Student. There are three variables - name, age and total (number of students). The variable total is declared as static and initialized with 0. Whenever a new object is created (constructor is called), the two variables name and age, are initialized and also the value of the variable total is incremented. 

Class StudentTest (StudentTest.java)
public class StudentTest {
    public static void main(String args[]) {
        Student s1 = new Student("John", 6);
        System.out.println("Student1 -");
        System.out.print("Name  : " + s1.name + ", ");
        System.out.print("age   : " + s1.age + ", ");
        System.out.println("Total : " + s1.total);
        System.out.println("");
        Student s2 = new Student("Adam", 9);
        Student s3 = new Student("Alex", 7);

        System.out.println("Student1 -");
        System.out.print("Name  : " + s1.name + ", ");
        System.out.print("age   : " + s1.age + ", ");
        System.out.println("Total : " + s1.total);
        System.out.println("");

        System.out.println("Student2 -");
        System.out.print("Name  : " + s2.name + ", ");
        System.out.print("age   : " + s2.age + ", ");
        System.out.println("Total : " + s2.total);
        System.out.println("");

        System.out.println("Student3 -");
        System.out.print("Name  : " + s3.name + ", ");
        System.out.print("age   : " + s3.age + ", ");
        System.out.println("Total : " + s3.total);
        System.out.println("");

        System.out.println("Student Total -");
        System.out.println(Student.total);
    }
}
In StudentTest class, we are creating 3 objects (instances) of class Student. When we print details, name and age are different for each student, but the value of the variable total is always 3, it doesn't matter which object is accessing it. The total variable is common to all the objects created. Here is the result of above program -

Student1 -
Name  : John, age   : 6, Total : 1

Student1 -
Name  : John, age   : 6, Total : 3

Student2 -
Name  : Adam, age   : 9, Total : 3

Student3 -
Name  : Alex, age   : 7, Total : 3

Student Total -
3

Please note the last statement of the program. We have used the class name directly to access the variable total. The result is same when you are accessing static variable using class name or object. Static variables can be accessed using class name, that's why these variables are also called class variables. Other variables are called instance variables.

No comments: