Java static Keyword
The static keyword in Java is used to create members (variables, methods, blocks, and nested classes) that belong to the class itself, rather than to any specific instance (object) of the class. A static member is shared by all objects of the class — it exists even before any object is created.
Why Use static?
Some data and behavior should be common to all objects of a class. For example, if a class tracks how many objects have been created, that count is shared — it doesn't make sense for each object to have its own copy. The static keyword handles this need.
1. Static Variables (Class Variables)
A static variable is declared with the static keyword. There is only one copy of a static variable regardless of how many objects exist. All objects share the same value.
class Counter {
static int count = 0; // shared by all objects
Counter() {
count++; // each new object increments the shared counter
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println("Objects created: " + Counter.count);
}
}Output:
Objects created: 3All three objects share the same count. It was incremented three times.
Accessing Static Variables
Static variables are accessed using the class name, not an object reference:
Counter.count // preferred – class name
c1.count // works but not recommended2. Static Methods
A static method belongs to the class and can be called without creating an object. It can only directly access static fields and call other static methods — it cannot use instance variables or this.
class MathHelper {
static int square(int n) {
return n * n;
}
static double circleArea(double radius) {
return 3.14159 * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Square of 5: " + MathHelper.square(5));
System.out.printf("Area: %.2f%n", MathHelper.circleArea(4.0));
}
}Output:
Square of 5: 25
Area: 50.27Note: The main method itself is static — that is why it can be called by the JVM without creating an object of the class.
Static vs Instance Members
| Feature | Static Member | Instance Member |
|---|---|---|
| Belongs to | Class | Object (instance) |
| Memory allocation | Once (when class is loaded) | Every time an object is created |
| Accessed via | Class name | Object reference |
| Shared? | Yes – same copy for all objects | No – each object has its own copy |
| Can access | Only static members | Both static and instance members |
3. Static Block
A static block is a block of code that runs once when the class is loaded into memory, before any object is created or any static method is called. It is typically used to initialize static variables that require more complex setup.
class Config {
static String appName;
static int version;
static {
appName = "MyApp";
version = 3;
System.out.println("Config initialized.");
}
static void display() {
System.out.println(appName + " v" + version);
}
}
public class Main {
public static void main(String[] args) {
Config.display();
}
}Output:
Config initialized.
MyApp v3The static block runs automatically when the class is first used.
4. Static Nested Class
A class defined inside another class with the static keyword is called a static nested class. It can be instantiated without creating an instance of the outer class.
class Outer {
static int outerValue = 100;
static class Inner {
void display() {
System.out.println("Outer value: " + outerValue);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}Output:
Outer value: 100Practical Example – Utility Class
Static methods are ideal for utility classes — classes that provide helper functions and are never instantiated. The built-in Math class is a perfect example.
class StringUtils {
static String capitalize(String word) {
if (word == null || word.isEmpty()) return word;
return Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
}
static boolean isPalindrome(String word) {
String reversed = new StringBuilder(word).reverse().toString();
return word.equalsIgnoreCase(reversed);
}
}
public class Main {
public static void main(String[] args) {
System.out.println(StringUtils.capitalize("jAVA")); // Java
System.out.println(StringUtils.isPalindrome("Madam")); // true
System.out.println(StringUtils.isPalindrome("Hello")); // false
}
}Important Rules
- Static methods cannot use
thisorsuper. - Static methods cannot directly access instance (non-static) variables or methods.
- Instance methods can access both static and instance members.
- Static blocks run once when the class is first loaded — before the main method.
Summary
- The
statickeyword makes a member belong to the class rather than to any object. - Static variables are shared across all instances of a class.
- Static methods can be called without creating an object.
- Static blocks run once when the class is loaded — useful for one-time initialization.
- Static nested classes can be used without an instance of the outer class.
- Static members are accessed using the class name.
