⏱️ 8 min read • Beginner Level • Lesson 47
The static keyword in Java is used to create class-level members. A static member belongs to the class, not to a specific object.
Before learning static, you should understand
Java class and objects,
Java class members, and
Java constructors.
In Java, static is a non-access modifier. It is used with variables,
methods, blocks, and nested classes.
A static member belongs to the class. That means it can be accessed using the class name without creating an object.
static.A Java class can have the following static members:
| Static Member | Meaning |
|---|---|
| Static Variable | A class-level variable shared by all objects. |
| Static Method | A class-level method that can be called without creating an object. |
| Static Block | A block that runs once when the class is loaded. |
| Static Nested Class | A nested class declared using the static keyword. |
A variable declared with the static keyword is called a
static variable. It is also called a class variable.
schoolName can be static because all students may belong
to the same school.
In this example, x is an instance variable, so every object gets its own copy.
But y is static, so it is shared by all objects.
class StaticVariableExample {
int x;
static int y;
void counter() {
x++;
y++;
System.out.println("x: " + x);
System.out.println("y: " + y);
}
public static void main(String[] args) {
StaticVariableExample obj1 = new StaticVariableExample();
obj1.counter();
StaticVariableExample obj2 = new StaticVariableExample();
obj2.counter();
}
}
Output:
x: 1 y: 1 x: 1 y: 2
x is an instance variable, so each object has its own copy.y is static, so it is shared by all objects.obj1.counter() runs, x becomes 1 and y becomes 1.obj2.counter() runs, its own x becomes 1, but shared y becomes 2.
A method declared with the static keyword is called a
static method. It belongs to the class and can be called using the class name.
this and super cannot be used inside a static method.class StaticMethodExample {
int x = 10;
static int y = 20;
static void show() {
System.out.println("Static method called");
System.out.println("y: " + y);
StaticMethodExample obj = new StaticMethodExample();
System.out.println("x: " + obj.x);
}
public static void main(String[] args) {
StaticMethodExample.show();
// Static method can also be called directly inside same class
show();
}
}
Output:
Static method called y: 20 x: 10 Static method called y: 20 x: 10
StaticMethodExample.show().
Calling static methods using objects is allowed but not recommended.
A static block, also called a static initializer block, is used to initialize static data. It executes automatically when the class is loaded.
main() method.class StaticBlockExample {
static {
System.out.println("Static block executed");
}
public static void main(String[] args) {
System.out.println("Main method executed");
}
}
Output:
Static block executed Main method executed
A class declared inside another class with the static keyword is called
a static nested class.
A static nested class can access static members of the outer class directly. To access non-static members, it needs an object of the outer class.
class Outer {
static String message = "Hello from static member";
static class Nested {
void show() {
System.out.println(message);
}
}
public static void main(String[] args) {
Outer.Nested obj = new Outer.Nested();
obj.show();
}
}
Output:
Hello from static member
A variable declared with static final is usually used to create constants.
It belongs to the class and its value cannot be changed after initialization.
class Circle {
static final double PI = 3.14159;
public static void main(String[] args) {
System.out.println("PI: " + Circle.PI);
}
}
Output:
PI: 3.14159
| Static Members | Instance Members |
|---|---|
| Belong to the class. | Belong to objects. |
| Shared by all objects. | Each object has its own copy. |
| Can be accessed using class name. | Accessed using object reference. |
| Loaded when class is loaded. | Created when object is created. |
static final.Can we override a static method in Java?
Answer: No. Static methods belong to the class, not to objects. A static method in a child class hides the parent method instead of overriding it.
this or super inside a static method.static keyword creates class-level members.static final is commonly used for constants.🧠 Test your understanding with a quick quiz
Topic: Static-keyword | Language: Java
🎉 Great job! Continue learning Java OOP step by step.