Master Java’s static Keyword: Variables, Methods, Blocks, Inner Classes & Imports
This article explains the five key uses of Java’s static keyword—including static variables, static methods, static initialization blocks, static inner classes, and static imports—provides clear code examples for each, and demonstrates how they affect class loading and access patterns.
Overview of the Java static keyword
The static modifier in Java can be applied to variables, methods, initialization blocks, inner classes, and import statements, allowing members to belong to the class itself rather than to individual instances.
1. Static variables
Static variables (also called class fields) are shared by all instances of a class and are initialized when the class is loaded.
public class MyClass {
static int staticVariable = 10;
public static void main(String[] args) {
// Access via class name
System.out.println(MyClass.staticVariable);
// Access via object (allowed but not recommended)
MyClass obj = new MyClass();
System.out.println(obj.staticVariable);
}
}2. Static methods
Static methods belong to the class and can be invoked without creating an object. They cannot directly access non‑static members.
public class MyClass {
static void staticMethod() {
System.out.println("This is a static method.");
}
public static void main(String[] args) {
// Preferred way
MyClass.staticMethod();
// Possible via object but discouraged
MyClass obj = new MyClass();
obj.staticMethod();
}
}3. Static initialization blocks
Static blocks run once when the class is loaded, typically to perform complex initialization of static variables.
public class InitializationOrder {
static int var1 = initializeVar1();
static int var2 = 20;
static {
System.out.println("Static Block 1");
System.out.println("var1 = " + var1);
System.out.println("var2 = " + var2);
var1 = 50; // reassign
}
static {
System.out.println("Static Block 2");
System.out.println("var1 = " + var1);
System.out.println("var2 = " + var2);
var2 = 100; // reassign
}
static int initializeVar1() {
System.out.println("Initializing var1");
return 10;
}
public static void main(String[] args) {
System.out.println("Main method");
System.out.println("var1 = " + var1);
System.out.println("var2 = " + var2);
}
}4. Static inner classes
A static inner class is created without an instance of the outer class and cannot access the outer class’s non‑static members.
public class OuterClass {
static class StaticInnerClass {
void display() {
System.out.println("Inside static inner class.");
}
}
public static void main(String[] args) {
OuterClass.StaticInnerClass inner = new OuterClass.StaticInnerClass();
inner.display();
}
}5. Static import
Static import allows static members to be used without qualifying them with the class name, making code shorter.
import static java.lang.Math.*;
public class MyClass {
public static void main(String[] args) {
System.out.println(sqrt(16)); // equivalent to Math.sqrt(16)
System.out.println(PI); // equivalent to Math.PI
}
}Understanding these five usages helps developers write clearer, more efficient Java code and control when and how class‑level resources are initialized and accessed.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
