The static keyword in Java is commonly used but often misunderstood. It helps define variables or methods that belong to the class itself rather than to individual objects. In this post, we’ll go through all the important uses of static — like static blocks, variables, methods, nested classes — using real examples and common interview questions.
Table of Contents
🧠 What is use of static
keyword in java ?
- When you mark something as
static
, it means it belongs to the class and not to any particular object. - Static members (variables, methods, blocks) can be accessed without creating an object.
- Useful for shared data, utility methods, and initializing logic.
📦 Types of static
in Java
- Static Variables
- Static Methods
- Static Blocks
- Static Nested Classes
📄 Code Example
class A {
// Static blocks run once when the class is loaded
static {
System.out.println("Static block1 is executed");
}
static {
System.out.println("Static block2 is executed");
}
static int code; // Class variable, shared across all objects
int x; // Instance variable, unique to each object
// Static method — can be called without an object
static void print() {
System.out.println("print is called in class A");
}
}
public class StaticExample {
public static void main(String[] args) {
A obj = new A();
obj.x = 90;
obj.code = 100; // Same as A.code
System.out.println(obj.x); // 90
System.out.println(obj.code); // 100
A obj2 = new A();
System.out.println(obj2.x); // 0 (default)
System.out.println(obj2.code); // 100 (shared)
// Change value of static variable
A.code = 200;
System.out.println(A.code); // 200
A.print(); // Calling static method
}
}
✅ Output:
Static block1 is executed
Static block2 is executed
90
100
0
100
200
print is called in class A
🏗️ Static keyword in java to define Class (Nested)
class Outer {
static class Inner {
void display() {
System.out.println("Static Nested Class Called");
}
}
}
public class StaticNestedExample {
public static void main(String[] args) {
Outer.Inner inner = new Outer.Inner();
inner.display();
}
}
✅ Output:
What is static nested class in java ?
🔸 A static nested class can be accessed without creating an instance of the outer class.
📚 Key Points to Remember about static keyword in java
- Static variables are shared across all instances.
- Static methods cannot access instance variables directly.
- Static blocks are used for initializing static variables or performing operations when the class is loaded.
- Static nested classes can be accessed without an outer object.
🎯 Interview Questions About Static Keyword in java
- Can we override static methods?
❌ No. Static methods belong to the class, not the instance. - When should you use a static block?
✅ To initialize static variables or run startup logic when the class loads. - What is the difference between static and instance methods?
- Static methods belong to the class and are called without objects.
- Instance methods need an object to be called and can access instance variables.
- Why is the
main
method static in Java?- Because the JVM needs to call it without creating an object of the class.
💡 Tips
- Use static for constants and utility functions.
- Be careful with static variables in multithreading.
- Don’t overuse
static
— it can reduce flexibility and testability.
✅ Summary
static
is a way to tie variables and methods to the class itself.- Helps manage shared data and utility methods.
- Often appears in frameworks, helper classes, and the Java
main
method.
You now have a solid understanding of static
in Java with examples and real-life use cases!
Leave a Reply