Skip to content
Sharp

Object-oriented programming · interview question

What does trying to access a non-static field from a static method in Java evaluate to?

Multiple choiceEasychapter-2-curatedpremiumoopfields-and-state
java
class Example {
  int instanceField;
  static void staticMethod() {
    System.out.println(instanceField); // Causes a compilation error
  }
}
Choose one answer
Answer and explanation

Correct answer

D. A compilation error

Why

Accessing a non-static field from a static method causes a compilation error because static methods do not have access to instance fields, which require an instance of the class.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →