static keyword in Java — Java Technical Interview Questions & Answers

static keyword in Java

static keyword in Java

  • static means it belongs to the class, not to each object. A static field is one shared copy — like a common counter. A static method can be called without new: Math.max, main.
  • main is static so the JVM can start it without making your class object first. static methods cannot use this or instance fields directly. You need an object for those.
  • Use static for shared utilities. Making everything static is usually poor design. Accessing instance variables from static without an object is a compile error.
  • Let's take this on the board with one tiny Main class — no extra files. static keyword in Java — output: 1. count belongs to the class, not to an object. main can use it without new Main(). Start from main, go line by line, and stop at each print. That output is the proof for static keyword.

Technical Interview Tip: Explain why main must be static.

Code Examples for static keyword in Java

Example

public class Main {
  static int count = 0;

  public static void main(String[] args) {
    count++;
    System.out.println(count);
  }
}

static keyword in Java — output: 1. count belongs to the class, not to an object. main can use it without new Main().

How to Explain static keyword in Java to an Interviewer

What does static mean?

Belongs to the class Not to each object

Why is main static?

JVM calls it without creating an object

Can a static method use this?

No