Skip to content
UniCircle
EasyConceptualClasses and objects

Static vs instance members

Question OOP-104

What is the difference between static and instance variables and methods? Why can't a static method directly access an instance variable? Give a sensible use of each.

Show hints

Ask whether the member belongs to the class or to each object.

Show the explanation

Model answer.

Instance members belong to each object.

  • Every object gets its own copy of an instance variable: two Student objects each have their own name and rollNo.
  • Instance methods run on a specific object and receive it implicitly as this, so they can read and modify that object's fields.

Static members belong to the class itself.

  • There is exactly one copy of a static variable shared by all objects of the class (within a running program).
  • Static methods are called on the class, e.g. Math.max(a, b) or Integer.parseInt("42"); no object is required.
  • Static initialisation happens when the class is loaded and initialised, before any instance is needed.

Why a static method can't touch instance fields directly
A static method has no this. If you write name inside a static method of Student, the question is "whose name?" — there may be zero objects, or thousands. You must pass or create an object and use obj.name. The reverse is fine: an instance method can freely use static members.

Good uses

  • Static variable: a counter of objects created, or a shared constant such as static final double GST_RATE = 0.18;.
  • Static method: pure utility functions that depend only on their arguments (Math.sqrt), or factory methods like List.of(...).
  • Instance members: anything describing one object's state and behaviour — an account's balance and its withdraw method.

Pitfalls

  • Mutable static state is effectively global state: it is shared across threads (needs synchronisation) and makes unit testing harder.
  • Static methods are not overridden polymorphically in Java; a subclass static method with the same signature only hides the parent's.

Common follow-up: "Why is main static?" So the JVM can call it without first creating an object of the class. A related trap: calling a non-static method directly from main without an object gives the compile error "non-static method cannot be referenced from a static context".

Original UniCircle question. Last reviewed 28 Oct 2026.