Skip to content
Sharp

Object-oriented programming · interview question

In the following code, what does 'this.price' refer to? public class Product { private double price; public Product(double price) { this.price = price; } }?

Multiple choiceMediumchapter-2-curatedpremiumoopthis-keyword
java
public class Product {
  private double price;
  public Product(double price) {
    this.price = price;
  }
}
Choose one answer
Answer and explanation

Correct answer

A. The class field 'price'.

Why

'this.price' refers to the class field 'price'. It is used to distinguish the class field from the constructor parameter with the same name.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →