Static Variable With Example In JAVA

Any variable when declared with the keyword “static” is known as static variable or class variable in JAVA. Static variable is used to fulfill the common properties of all objects. For example: institute name of students is common for all students so it will be declared as static variable in JAVA. The static variable gets memory at class loading time only once in class area.

Syntax of static variable:

static <DataType> <variable_name>;

For example:

Below age is a static variable.

static int age;

Initialization of Static Variable:

  1. Static variables are initialized when class is loaded.
  2. Static variables in a class are initialized before any object of that class can be created.
  3. Static variables are initialized before any static method of the class runs.

When To Create Static Variable:

Problem Without Using Static Variable

First let’s take a program example without using static variable and see what problem occur. Suppose a college name GGGI wants to store students details name and roll number.

class Student
    {
        int rollno;
        String name;
        String college="GGGI";
    }

Problem: Suppose there are 600 students in GGGI institute. When object is created, all instance data members will get memory each time. All students have its unique rollno and name so instance data member is good. But here, institute refers to the common property of all objects. So if we make it static, this field will get memory only once in a class area at class loading time.

Solution Using Static Variable:

The below program has a class Student in which we use static variable college. This variable is common to all students so we make it static. The student information will be displayed with different roll no and names but same college. The advantage to make college variable static is that it save memory as it loads once in a class area at class loading time:

class Student

{
    int rollno;
    String name;
    static String college ="GGGI"; //Static Variable gets memory once

    /*Constructor of Student class*/
    Student(int r,String n)
    {
        rollno = r;
        name = n;
    }
    
    /*Method For Displaying Student Details*/
    void display()
    {
        System.out.println(rollno+" "+name+" "+college); // print the value of roll no, name and college
    }

    public static void main(String args[])
    {
        Student s1 = new Student(101,"Gagan");
        Student s2 = new Student(102,"Raman");
        s1.display();  // call the display function using the s1 object
        s2.display();   // call the display function using the s2 object
    }

}

Output:

101 Gagan GGGI
102 Raman GGGI

 Importance Of Static Variable:

With the help of static variable we make our program memory efficient. It means static variable saves memory.


Important Points About Static Variable To Remember:

  • Static variables are also known as Class variables which are declared with the “static” keyword in a class.
  • A single copy of each variable per class is to be shared by all instances of the class.
  • Static variables are stored in static memory. Static variables are rarely used other than it is declared final and used as either public or private constants.
  • When the program starts static variables are created and when the program stops, static variables are destroyed.
  • Default values of static variables are same as instance variables. For numbers the default value is 0, for Boolean the default value is false and for object references the default value is null.
  • When static or class variables are declared as public static final, then variables names are all in upper case. The naming syntax is the same as instance and local variables in case of static variables are not declared public and final.
  • Static variables can be accessed by calling with the class name. It does not need any object.
  • Static variables are initialized only one time, i.e. at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.

One thought on “Static Variable With Example In JAVA”