Skip to content
Sharp

Object-oriented programming · interview question

What will happen if 'this' is misused in the following code? public class Store { private String name; public static void main(String[] args) { this.name = "Online Store"; } }?

Multiple choiceHardchapter-2-curatedpremiumoopthis-keyword
java
public class Store {
  private String name;
  public static void main(String[] args) {
    this.name = "Online Store";
  }
}
Choose one answer
Answer and explanation

Correct answer

C. Compilation error due to 'this' in a static context.

Why

Using 'this' in a static method like 'main' results in a compilation error because 'this' refers to an instance, and static methods don't belong to any particular instance.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →