Java Static Explained: Variables, Methods, and Blocks – Hands‑On Examples
In Java, the static keyword appears in three distinct contexts: static variables, static methods, and static blocks. Each plays a unique role in the lifecycle of a class and its instances.
Static Variables in Java
A static variable is a class-level field that is initialized only once when the class is first loaded. Unlike instance variables, a single copy of a static variable is shared by all objects of that class.
- Only one copy exists, shared across all instances.
- Accessible directly via
ClassName.variableNamewithout creating an object. - Initialized before any instance fields are set.
Syntax:
public static int count;
Static Methods in Java
Static methods belong to the class rather than to any particular instance. They can be invoked without creating an object and can only access static data.
- Can call other static methods but not instance methods.
- Invoked with
ClassName.methodName(). - Cannot use
thisorsuperbecause they are not tied to an instance.
All main methods are static so that the JVM can launch an application before any objects exist.
Practical Example: Using Static Variables and Methods
Below is a simple program that demonstrates how a static counter is shared across multiple Student objects.
public class Demo {
public static void main(String[] args) {
Student s1 = new Student();
s1.showData();
Student s2 = new Student();
s2.showData();
// Student.b++; // uncomment to modify the counter
// s1.showData();
}
}
class Student {
int a; // instance variable, defaults to 0
static int b; // static counter, initialized to 0 once
Student() {
b++; // increment counter each time a Student is created
}
public void showData() {
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
}
Compile and run with java Demo to see the following output:
The diagram below illustrates how reference variables and objects are created, and how the static variable b is shared across instances.
To access the static variable from outside the class, use ClassName.variableName:
Student.b++; // increments the shared counter
s1.showData(); // will show updated value of b
Attempting to access an instance variable from a static context results in a compilation error:
error: non-static variable a cannot be referenced from a static context
a++;
This happens because static methods cannot directly interact with instance fields.
Static Blocks in Java
A static block is a special block of code that executes once when the class is first loaded into the JVM. It’s ideal for initializing static fields that require more than a simple assignment.
class Test {
static int a;
static int b;
static {
a = 10;
b = 20;
}
}
Running the following program demonstrates the output of a static block:
public class Demo {
static int a;
static int b;
static {
a = 10;
b = 20;
}
public static void main(String[] args) {
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
}
Output:
Value of a = 10 Value of b = 20
Java
- Java Methods: How to Define, Call, and Use Them Effectively
- Mastering Java Polymorphism: Concepts, Examples, and Best Practices
- Understanding Java Nested Static Classes: Usage, Differences, and Examples
- Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
- Mastering Java’s ObjectInputStream: A Comprehensive Guide
- Mastering Abstraction in OOP: Java Abstract Classes & Methods Explained
- Polymorphism in Java: A Comprehensive Guide with Practical Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Java Interfaces Explained: How to Define and Implement Them with Practical Examples
- Interface vs Abstract Class in Java: How to Choose the Right Abstraction