Open In App

Static Variables in Java

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we use the static variable most of the time:

  • Storing constant values or default values that can be used across different instances.
  • The Static variable is used to store the data of class instances. To record the number of times a class instance is created.

Important points:

  • Class-Level: We can create static variables at the class level only. To know more, refer here.
  • Shared Among Objects: The static blocks and static variables are executed in the order they are present in a program. It means if a static variable is modified by any instance, the changes will show in all other instances.
  • Accessed through Class Name: Static variables can be called directly with the help of a class only; we do not need to create an object for the class in this.
  • Initialization Order: The static variables are initialized before the static blocks, and when the static method or block refers to a static variable, then it will use its initialized value.

Example: This example demonstrates how to use a static variable among different methods.

Java
// Java program to demonstrate execution
// of static blocks and variables
class Geeks
{
    // static variable
    static int a = m1();

    // static block
    static
    {
        System.out.println("Inside static block");
    }

    // static method
    static int m1()
    {
        System.out.println("from m1");
        return 20;
    }

    // static method(main)
    public static void main(String[] args)
    {
        System.out.println("Value of a : " + a);
        System.out.println("from main");
    }
}

Output
from m1
Inside static block
Value of a : 20
from main

Explanation: In the above Java program, we use the static variable which store the value which is calculated by the m1() method.


Difference Between Static Variable and Instance Variable

Aspect

Static Variable

Instance Variable

Definition

The static variable is is belong to the class and It shared the same instance in all the reference where it is used.

The instance variable is belong to the instance of a class (object).

Memory

The static variable is allocated on the time of class loading in the memory only once.

The instance variable is created when we create an object of a class.

Modification

If we update the value of the static variable it will reflect to all the instances where it is In use.

The instance variable has the separate copies in each of it’s instances.

Access

We can directly access it without creating an object of a class.

The instance variable is only access via object of the class.

Scope

The scope of the static variable is in the entire class. We can access through all the instances of the class.

It’s scopeis limited to the specific instance of the defined class.



Next Article
Article Tags :
Practice Tags :

Similar Reads