Applying the static Keyword
OCP Exam Topic — Understand static fields, static methods, static initializer blocks, and static imports. Covered in Chapter 5 of OCP Java SE 21 Developer Study Guide (Selikoff & Boyarsky).
What static Means
The static keyword marks a member as belonging to the class itself rather than to any individual instance. Static members are shared across all instances and exist for the lifetime of the class.
Static Fields
A static field is a single variable shared by all instances of the class.
javapublic class Counter {
private static int count = 0; // one copy, shared by all instances
public Counter() {
count++;
}
public static int getCount() {
return count;
}
}
Counter a = new Counter();
Counter b = new Counter();
Counter c = new Counter();
System.out.println(Counter.getCount()); // 3Because count is static, every new Counter() increments the same variable regardless of which instance is created.
Static Methods
Static methods belong to the class, not to an instance. They are called on the class name and cannot use instance fields or the this keyword.
javapublic class MathHelper {
public static int square(int n) {
return n * n;
}
}
System.out.println(MathHelper.square(5)); // 25Rules for static methods:
- They can access only other static members directly.
- They cannot access instance fields or call instance methods without an object reference.
- They cannot use
thisorsuper.
javapublic class Example {
private int instanceVal = 10;
private static int staticVal = 20;
public static void show() {
System.out.println(staticVal); // OK
// System.out.println(instanceVal); // compile error
}
}Static Initializer Blocks
A static initializer block runs once when the class is first loaded by the JVM. It is useful for complex initialization of static fields that cannot be done with a simple assignment.
javapublic class Config {
public static final String DEFAULT_HOST;
public static final int DEFAULT_PORT;
static {
DEFAULT_HOST = System.getProperty("app.host", "localhost");
DEFAULT_PORT = Integer.parseInt(System.getProperty("app.port", "8080"));
System.out.println("Config initialized");
}
}Multiple static blocks in the same class execute top to bottom in declaration order.
Accessing Static Members via Class Name
The correct way to call a static member is through the class name:
javaMath.abs(-5); // correct
Math.PI; // correctJava permits calling static members via an instance reference, but this is misleading because the instance is irrelevant — the compiler resolves it to the class anyway.
javaCounter c = new Counter();
c.getCount(); // compiles, but misleading — avoid this
Counter.getCount(); // preferred — makes static nature clearOn the OCP exam, watch for code that calls a static method on a null reference — this does not throw a NullPointerException because no instance is involved:
javaCounter c = null;
System.out.println(c.getCount()); // compiles and runs — prints the static valueStatic Imports
The import static statement imports individual static members or all static members of a class so they can be used without the class name qualifier.
javaimport static java.lang.Math.PI;
import static java.lang.Math.sqrt;
public class Circle {
public double area(double radius) {
return PI * radius * radius;
}
public double hypotenuse(double a, double b) {
return sqrt(a * a + b * b);
}
}Importing all static members with a wildcard:
javaimport static java.lang.Math.*;
double result = pow(2, 10); // no Math. prefix neededCaution: static imports that shadow other names or create ambiguity are a compile error. Prefer importing specific members over using wildcards in production code.
Common Pitfalls
| Pitfall | Explanation |
|---|---|
| Calling static via instance reference | Compiles but hides the static nature; avoid it |
Using this in a static method |
Compile error — no instance context |
| Accessing instance field in static method | Compile error — static method has no this |
| Ambiguous static import | Compile error when two imported classes have the same static member name |
Key Points to Remember
staticmembers belong to the class, not to instances; they are shared across all objects.- Static methods cannot access instance fields or use
this/superwithout an object reference. - Static initializer blocks run once when the class is loaded; multiple blocks execute in order.
- Access static members through the class name, not an instance reference.
import staticlets you use static members without the class name prefix; wildcards are allowed but can create ambiguity.- Calling a static method via a
nullreference does not throwNullPointerException.