Why is this.name = name; useful in a constructor?
this.name names the object's variable, while the bare name names the parameter that is hiding it.
Explanation
When a parameter shares a name with an instance variable, the parameter wins inside the method. this is what lets the code reach past the parameter to the object's own variable, which makes it possible to give both the same descriptive name.
- A is not correct: a class variable is created by a declaration with
static, not by an assignment. - B is not correct: a constructor has no return type and returns nothing.
- D is not correct: a single
=assigns; comparison would need==or a method call.