0

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?

3
  • 2
    It is not about using of "this" thus not a duplicate, but more of the best practices when you have an alternative. I personally think there is no reason to use "this" here and duplicated variable names. Commented Dec 13, 2017 at 14:52
  • 2
    I'd say about half or a third of this question overlaps with a small portion of one of those answers. It's not a clear 1 to 1 duplicate Commented Dec 13, 2017 at 14:53
  • 1
    It still remains to the close voters if a question is indeed a duplicate or off topic. As long as the question is open and if you personally don't think it is a duplicate or off topic or any of this, then you should answer the question. There are many cases where just some think it is duplicate/OT (not enough for closing) and in that case answers are welcome. And while I'm not a fan of down-voting good answers to bad questions you should certainly wait until the close is accepted. Commented Dec 13, 2017 at 16:50

5 Answers 5

1

As for why this is actually pretty common: IDEs provide automatic assignments. So what you do in Eclipse for example is type

class Address {
    public Address(String city, String state, String country) {}
}

and Eclipse will warn you that the parameters city, state and country are not used in the method. So what you can do is trigger an automatic fix, one of which is "assign to new fields" which will create the fields in the class and assign them in the constructor. The field names will then be the same as the parameters, and therefore need to be prefixed with this. so they reference the fields, not the parameters.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there a reason for using this.city=city here?

In this case, using this.city = city; is to prevent ambiguity to readers (and compiler)

The alternative is city = city;, but it is not obvious as to which city is being set

(and in this case does not modify the class variable)

Related: Answer to When should I use “this” in a class?, case 1

Isn't it equal to having different local variable names in the method

Yes, using different variable names may be cleaner

(some people prefer the same names, and just matching, I prefer first letters: city = c)

What are benefits and drawbacks of the second version in comparison to the first one?

It's a style, if you're programming with others it's best to have the same style

If the company you're working for does it one way, and you start doing it a different way, it will be a lot harder to maintain the codebase

Related: Is Programming Style important? How Important?

7 Comments

Actually, i for city is not cleaner, but city = cityName is
I suspect i, j, and k were just examples that it could be anything, if they were actually used then it'd be a lot harder to follow (than c or cityName)
"The alternative is city = city;, but it is not obvious as to which city is being set" - That's no alternative, that is completely wrong.
@Tom Alternative to using this. while still keeping both variables as city, it does not retain the same functionality, but yet plenty of people try using it an come back with unexpected results. It is not intuitively obvious as to which city is being set and does not throw an error
Ok, the edit makes it clear what you mean. It first looked like you suggest that as a working alternative (which might not be obvious to understand).
|
0

Notice that you have city as a field in the class, and also it's a parameter being passed by the function. In order to assign the passed parameter to the city field, this.city is used

Comments

0

The reason why they are using this.city here is because, as you said, the local parameter name city is ambiguous to the class variable.

Another point of using this to access class variables is that it can make the code more readable because you instantly know its a class variable even if you don't see the declaration.

Comments

-1

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?

It is.
But it may make clearer to use the same name in parameters as in fields as you assign parameters to fields.

A city field represents a city.
Why using another name in the parameter ?
Personally, representing it by i or cityP in the parameter name makes it less clear. After, it's a matter of taste.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.