Interview Q&A / Java

IntermediateIn-depth3 min readUpdated 2 Oct 2026

What does the static keyword mean in Java?

SHORT ANSWER

static means a member belongs to the class itself, not to any one object. A static variable has one shared copy, a static method can be called without creating an object, a static block runs once when the class is loaded, and a static nested class does not need an outer object.

Explain it simply

Think of a college hostel. Every student has their own bed, cupboard and ID card. Those are like instance variables: each object gets its own copy. But there is one notice board for the whole hostel. Everyone reads the same board, and if the warden changes the mess timing on it, every student sees the change. That shared board is a static variable.

A static method is like the hostel office phone number. You do not need to know any particular student to call it; it belongs to the hostel itself.

Example

java
public class Student {
    static String college = "NIT Jamshedpur";   // one copy, shared by all
    static int count = 0;                        // shared counter

    String name;                                 // one copy per object

    Student(String name) {
        this.name = name;
        count++;
    }

    static int totalStudents() {                 // no object needed
        return count;
    }

    public static void main(String[] args) {
        new Student("Asha");
        new Student("Vikram");
        System.out.println(Student.totalStudents()); // prints: 2
        System.out.println(Student.college);         // prints: NIT Jamshedpur
    }
}

How it works

static can be used in four places:

  1. Static variable: one copy per class, created when the class is loaded. Every object reads and writes the same value.
  2. Static method: called with the class name, like Math.max(3, 7). It has no this, so it cannot directly use instance variables or instance methods.
  3. Static block: static { ... } runs once, when the class is first loaded and initialised. Used for setup such as filling a lookup table.
  4. Static nested class: a class inside another class that does not hold a reference to an outer object. The builder pattern often uses one.

Also, import static java.lang.Math.max; lets you write max(a, b) without the class name.

main is static because the JVM must call it before any object of your class exists.

Real-world use

  • Constants: static final double GST_RATE = 0.18;
  • Utility methods that need no object state: Math.abs, Collections.sort, Integer.parseInt, or your own DateUtils.format(...).
  • Factory methods such as List.of(...) and LocalDate.now().
  • A shared counter or cache, used carefully.

Common mistakes

  • Using instance variables inside a static method. The compiler gives "non-static variable cannot be referenced from a static context".
  • Making mutable data static "for convenience". Shared state makes code hard to test, and if several threads change it, you get race conditions.
  • Thinking static methods can be overridden. A child class can declare a static method with the same signature, but that only hides the parent's method; which one runs depends on the reference type, not the object.
  • Calling a static method through an object (s.totalStudents()). It compiles but misleads the reader; use the class name.

What to say in the interview

"static means the member belongs to the class rather than to any object. A static variable has a single shared copy, a static method can be called with the class name and has no this, so it can't touch instance fields directly. A static block runs once when the class is loaded, and a static nested class doesn't need an instance of the outer class. main is static so the JVM can call it without creating an object. I use static for constants and stateless utility methods, and I avoid static mutable state because it's hard to test and not thread-safe."

Follow-up questions

  • Can a static method be overridden?
  • Why is main static?
  • Can we access a non-static variable from a static method?
  • In what order do static blocks, instance blocks and constructors run?

Go deeper

Read these once the basics above feel easy.

Initialisation order matters in tricky questions. When a class is first used, its static variables and static blocks run once, top to bottom, in the order they appear. Then, each time you create an object, instance variable initialisers and instance blocks run, followed by the constructor body. For a subclass, the parent's static initialisation happens before the child's, and when an object is created the parent's constructor finishes before the child's constructor body runs. A static field is not stored in any object; its value lives with the class, so it stays alive as long as the class stays loaded, which is a common source of memory leaks when a static collection keeps growing.

#java#static#keywords#memory

Related questions

Practise more Java questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Java quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.