The static keyword in Java is used to define
members that belong to the class rather than to individual
objects. It can be used with variables, methods, initializer
blocks, and nested classes.
A static member is associated with the class itself. You can usually access a static field or method using the class name.
class Student {
static String college = "ABC College";
public static void main(String[] args) {
System.out.println(Student.college);
}
}
ABC College
Here, college is a static variable
associated with the Student class.
A static variable is shared by all instances of its class. There is one class-level field rather than a separate copy for each object.
class Student {
String name;
static String college = "ABC College";
Student(String name) {
this.name = name;
}
void display() {
System.out.println(name + " - " + college);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul");
Student s2 = new Student("Priya");
s1.display();
s2.display();
}
}
Rahul - ABC College
Priya - ABC College
A static method belongs to the class. It can be called without creating an instance of that class.
class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = Calculator.add(10, 20);
System.out.println(result);
}
}
30
The method add() is called using the
class name, Calculator.
| Static Variable | Instance Variable |
|---|---|
| Associated with the class. | Associated with each object. |
| One shared class-level field. | Each object has its own field. |
| Can be accessed using the class name. | Normally accessed through an object. |
class Demo {
static int count = 0;
int number = 0;
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.count++;
d1.number++;
d2.count++;
d2.number++;
System.out.println(Demo.count);
System.out.println(d1.number);
System.out.println(d2.number);
}
}
2
1
1
The static field is shared, while each object has its own instance field.
A static method does not have an automatic reference to a particular object. Therefore, it cannot directly access instance variables or instance methods.
class Demo {
int number = 10;
static void show() {
// System.out.println(number);
// Compilation error
}
public static void main(String[] args) {
Demo d1 = new Demo();
System.out.println(d1.number);
}
}
10
To access the instance field inside a static method,
use an object reference such as
d1.number.
The Java entry-point method is commonly declared
public static void main(String[] args).
The static modifier allows the runtime to invoke
this entry point without first creating an instance
of the class.
class Demo {
public static void main(String[] args) {
System.out.println("Java Program Started");
}
}
Java Program Started
Meaning of the declaration:
public — accessible to the launcher.static — associated with the class.void — returns no value.main — entry-point method name.String[] args — receives command-line arguments.A static initialization block is used to initialize static data. It runs when the class is initialized, before its first active use that triggers initialization.
class Demo {
static int number;
static {
number = 100;
System.out.println("Static block executed");
}
public static void main(String[] args) {
System.out.println(number);
}
}
Static block executed
100
The static block assigns a value to the static field before the main method prints it.
A static method can create an object and use its instance members through that object's reference.
class Student {
String name = "Rahul";
void display() {
System.out.println(name);
}
static void showStudent() {
Student s1 = new Student();
s1.display();
}
public static void main(String[] args) {
Student.showStudent();
}
}
Rahul
The static method creates a Student object and calls its instance method through that object.
Static methods can be inherited by subclasses when access rules allow it. A subclass can also declare a static method with the same signature, which hides the superclass method rather than overriding it.
class Parent {
static void show() {
System.out.println("Parent static method");
}
}
class Child extends Parent {
static void show() {
System.out.println("Child static method");
}
public static void main(String[] args) {
Parent.show();
Child.show();
}
}
Parent static method
Child static method
Java allows a static nested class to be declared inside another class. Unlike a non-static inner class, it does not require an instance of the enclosing class to be created.
class Outer {
static class Inner {
void display() {
System.out.println("Static nested class");
}
}
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}
Static nested class
The nested class is accessed using
Outer.Inner.
Mistake 1: Using this in a static method
static void show() {
// this.display(); // Compilation error
}
Mistake 2: Accessing an instance field directly
class Demo {
int number = 10;
static void show() {
// System.out.println(number);
// Compilation error
}
}
Mistake 3: Treating a shared field as object-specific
class Counter {
static int count = 0;
}
Changing Counter.count affects the
single shared static field, not a separate copy
belonging to each object.
class Student {
String name;
static int totalStudents = 0;
Student(String name) {
this.name = name;
totalStudents++;
}
void display() {
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul");
Student s2 = new Student("Priya");
Student s3 = new Student("Amit");
s1.display();
s2.display();
s3.display();
System.out.println(
"Total Students: " + Student.totalStudents
);
}
}
Name: Rahul
Name: Priya
Name: Amit
Total Students: 3
The static variable counts how many Student objects have been constructed during this program's execution.
collegeName and print it using the class name.
add()
that returns the sum of two integers.
1. What does a static variable belong to?
2. Can a static method directly use this?
3. What is a static initialization block used for?