Static Modifier in Java
In this article, we will discuss the static non-access modifier in Java. Non-access modifiers change the default behavior of the Java class and its members.
As explained below, the non-access modifier static in Java can be used with variables, methods, classes, and interfaces.
Static Variables in Java
As we know, variables are associated with the class. Similarly, static variables belong to the class, and they are common to all the instances (reference variable of a class) and are shared by all the class objects in Java. If we don't want a variable to be object-specific, we can make it static.
Let us see this with an example,
package StaticModifier;
class Employee {
int empid;
String empName;
String ceo;
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.empid = 1;
emp1.empName = "ABC";
emp1.ceo = "XYZ";
Employee emp2 = new Employee();
emp2.empid = 2;
emp2.empName = "DEF";
emp2.ceo = "XYZ";
emp1.display();
emp2.display();
}
}
When this program executes, it will result in
1 : ABC: XYZ
2 : DEF: XYZ
As in one organization CEO is the same for all the employees, so we will change the visibility of the variable CEO to static as its values are the same for all objects irrespective of the object.
The above scenario can be implemented as,
package StaticModifier;
class Employee {
int empid;
String empName;
static String ceo;
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.empid = 1;
emp1.empName = "ABC";
emp1.ceo = "XYZ";
Employee emp2 = new Employee();
emp2.empid = 2;
emp2.empName = "DEF";
emp2.ceo = "XYZ";
//this value will be replaced as ceo is a static variable
emp2.ceo = "PQR";
emp1.display();
emp2.display();
}
}
When this program executes, it will result in,
1: ABC : PQR
2: DEF : PQR
Since the CEO is a static variable, the value will be the same for all objects irrespective of the object. Hence we get PQR as the CEO name attribute.
Points to remember
Since static variables belong to a class, we can use both object names and class names - both will work. But the preferred way to access them is by using the class name. If we make a variable static, it will not be any more for a particular object; it will be the same for all the objects. Normal variables are loaded with heap memory, but static variables are loaded with classloader memory. If a variable is static, the value will be the same for all objects, irrespective of the object.
Static Block in Java
A static block is a special block in Java that can be used to initialize a static variable. It can be easily explained when referenced with static or non-static variables since the above code describes the need for the static keyword in Java.
Let us demonstrate it with an example,

Mariusz PostolPosted Jul 19, 2024, 10:58 AM
You said "they are common to all the instances" - do we need to use a reference to an instance to select a static member or selection is based on the type identifier?
love ilovePosted Oct 28, 2020, 2:15 AM
Nice explanation and thank you for this tutorial.