HuntExams Academy logo
JavaBeginner#fundamentals

What is the difference between static and instance methods?

Answer

Static methods belong to the class and can be called without an object instance; they cannot access instance (non-static) fields directly. Instance methods operate on an object's state and require an instance to invoke.

Example

class MathUtil { static int square(int x) { return x*x; } }
MathUtil.square(5); // no instance needed, returns 25

Detailed explanation

In Java, a static method belongs to a class, while an instance method is invoked on a particular object. Declare a static method using the static keyword and normally call it through the class name, such as MathUtil.square(5). An instance method has no static modifier and is called through an object reference, such as account.getBalance(). The key distinction is whether the method receives an implicit current object, represented by this, rather than whether the method accepts arguments or returns a value.

A static method has no this reference, so it cannot directly access instance fields, call instance methods without an object reference, or use this or super. However, it can access static members directly and access instance members through an explicit object reference, subject to access rules. For example, a static method receiving an Account object can call account.getBalance(). An instance method has access to its current object's state and can also access static members. Thus, static does not mean that a method cannot interact with objects; it means that no current object is supplied automatically.

Under the hood, an instance method invocation supplies a receiver object; a static invocation does not. Overridden instance methods support runtime polymorphism: the implementation executed depends on the receiver's actual class. Static methods are not overridden. If a subclass declares a static method with the same signature as an inherited static method, it hides that method, and selection depends on the compile-time type or class qualifier. Java permits calling a static method through an object reference, but using the class name communicates its meaning more clearly.

Choose static for behaviour that does not depend on a particular object's state, such as square(int x), which returns x * x. Choose an instance method for behaviour tied to an object, such as withdrawing money from a specific account. Interviewers usually expect you to explain object access, invocation syntax, and overriding versus hiding. Avoid claiming that static methods are inherently faster, thread-safe, or unable to change state: they can modify shared static data, which may require synchronisation.

Key points to remember

  • Static methods belong to the class; instance methods execute with a receiver object.
  • Call static methods using ClassName.method() and instance methods using object.method().
  • Static methods have no this reference and need an explicit object reference to access instance members.
  • Instance methods can access both instance members and static members.
  • Instance methods can be overridden; static methods are hidden, not overridden.

Common mistakes candidates make

  • Saying static methods cannot access instance members at all; they can do so through an object reference.
  • Claiming that static methods participate in runtime overriding like instance methods.
  • Assuming static methods are automatically thread-safe or cannot modify state.

Likely follow-up questions

Can a static method be called through a null reference?

Yes, if the reference has a suitable declared type, a static call can compile and execute even when that reference is null because no receiver object is needed. An instance method call on null throws NullPointerException; use the class name for static calls to avoid confusion.

Can static and instance methods be overloaded?

Yes, both can be overloaded by declaring methods with different parameter lists. However, static alone does not distinguish signatures, so a class cannot declare a static and an instance method with the same name and parameter types.

Why is the traditional main method static?

The traditional entry point, public static void main(String[] args), lets the Java launcher invoke it without first constructing an application object. It can create objects and call their instance methods when execution begins.

Interview tip: Start with the receiver-object distinction, illustrate it with MathUtil.square(5) versus account.getBalance(), and finish with overriding versus hiding.

Related Interview Questions

1
JavaBeginner#oop

What is a constructor and how does constructor overloading work?

Open
2
JavaIntermediate#oop

What is the difference between instance initializer blocks and constructors?

Open
3
JavaBeginner#object#fundamentals

What is the difference between == and equals() when comparing objects that don't override equals()?

Open