Skip to content
Sharp

Object-oriented programming · interview question

Why does a constructor often use this.name = name;?

Multiple choiceMediumchapter-2-curatedpremiumoopthis-keyword
Choose one answer
Answer and explanation

Correct answer

A. To distinguish the field on the current object from the constructor parameter

Why

When the parameter and field share the same name, this.name refers to the object's field and name refers to the parameter value. That is one of the first common uses of this in Java.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →