I am learning Java by myself. I read this code and I need an explanation.
Why in this piece of code (copied from https://www.javatpoint.com/aggregation-in-java) this
has been used?
Isn't it equal to having different local variable names in the method such as:
public Address(String i, String j, String k)
and just use city=i
instead?
Is there a reason for using this.city=city
here?
Thanks.
public class Address {
String city,state,country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
In fact, I would write the code as:
public class Address
{
String city,state,country;
public Address(String tempCity, String tempState, String tempCountry) {
mycity = tempCity;
state = tempState;
country = tempCountry;
}
}
What are benefits and drawbacks of the second version in comparison to the first one?