Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
A calculator on your phone — you don't create an account or log in. You just open and use it directly. No setup. No object. That's a static method.
What is a Static Method?
A static method belongs to the CLASS — callable directly without creating an object.
java
QUBITS OF DPK
Why is main() Static?
java
QUBITS OF DPK
JVM starts the program before ANY object exists. It needs to call main() directly via class. So main MUST be static — callable without an object.
Rules of Static Methods
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"A static method belongs to the class — callable via class name without creating an instance. main() is static because JVM calls it before any object exists. Static methods can access static variables directly but cannot access instance variables or use 'this' since they have no object context. They're ideal for stateless utility operations, factory methods, and helper functions. Instance methods can freely access both static and instance members."
Interview Questions & MAANG-Level Answers
Q1. Why is main() declared as static?
JVM needs to call main() to start the program. At startup, no objects exist yet — no class has been instantiated. Static methods can be called without an object, just via the class name: Main.main(args). If main() were an instance method, JVM would need to create an instance first, but it doesn't know how (what constructor? what arguments?) — a chicken-and-egg problem. static solves this by making main directly callable on the class.
Q2. Can static methods access instance variables?
No, directly. Static methods have no this reference — they don't belong to any object. Instance variables need an object to exist. static void show() { System.out.println(name); } — compile error. Fix: create an object inside the static method: static void show() { MyClass obj = new MyClass(); System.out.println(obj.name); }. This is why utility classes (Math, Arrays) only have static methods and no instance state.
Q3. Can instance methods access static variables?
Yes, freely. Instance methods have access to both instance and static members. void showCount() { System.out.println(totalUsers); } — perfectly valid. Instance methods can also call static methods. The reverse is not true: static cannot access instance without an object reference.
Q4. What is the difference between a utility class and a regular class?
A utility class is a class with ONLY static methods and no instance state — it exists purely to group related functions. Typically has a private constructor to prevent instantiation. Examples: Math, Arrays, Collections, Objects. A regular class has instance state and behavior, meant to be instantiated. In production: create utility classes for stateless operations like validation, formatting, encryption.
Q5. What is method hiding (static method in inheritance)?
When a child class defines a static method with the same signature as a parent's static method, it's called method hiding — NOT overriding. The method called depends on the REFERENCE TYPE (compile time), not the object type. Animal a = new Dog(); a.staticMethod() calls Animal's static method, not Dog's. This is opposite to instance method overriding where runtime type decides. @Override annotation on static method would cause compile error.