I don't understand the following:
In the code found below, Visual Studio tells me, that I can simplify my code by deleting the second this
keyword in the constructor.
But why can't then I do away with the first this
keyword?
Both variables were declared outside the constructor, in the class, so both will be "overridden"* for the instance.
If I remove both this
keywords, then VS will complain that the first assignment is made to same variable, but not for the second.
The only obvious difference to me is the second variable is an array, but I do not understand how would that make it any different?
*I suspect override is not the correct term here.
public class CelestialObject {
CelestialBody[] celestialBodies;
int celestialBodyCount;
public CelestialObject(int celestialBodyCount = 2) {
this.celestialBodyCount = celestialBodyCount;
this.celestialBodies = new CelestialBody[celestialBodyCount];
}
}
You cannot do away with the first this
keyword because you have a parameter with the same name as your field celestialBodyCount
:
int celestialBodyCount; // The field
And
public CelestialObject(int celestialBodyCount = 2) { // The parameter.
The this
keyword is required to indicate that the field is being assigned to. Otherwise the parameter takes precedence over the field, as explained in 3.7.1 Name hiding:
... the scope of an entity may include declarations that introduce new declaration spaces containing entities of the same name. Such declarations cause the original entity to become hidden.
And then 3.7.1.1 Hiding through nesting:
Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations.
In this case the parameter celestialBodyCount
hides the member celestialBodyCount
.
Of course, if you do remove the this
the compiler will helpfully give you the following warning:
Assignment made to same variable; did you mean to assign something else?
This warning almost always indicates a bug and should always be cleaned up.
The difference is that you've got a parameter called celestialBodyCount
- which means that within the constructor, the identifier celestialBodyCount
refers to the parameter, so to access the field you have to qualify it with this
.
You don't have a parameter (or other local variable) called celestialBodies
, so that identifier already refers to the field, without any qualification required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With