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?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Artemis-a
  • 45
  • 4
  • 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. – Artemis-a Dec 13 '17 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 – phflack Dec 13 '17 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. – Zabuzard Dec 13 '17 at 16:50

5 Answers5

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?

phflack
  • 2,729
  • 1
  • 9
  • 21
  • Actually, `i` for `city` is not *cleaner*, but `city = cityName` is – Andrew Tobilko Dec 13 '17 at 14:46
  • 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`) – phflack Dec 13 '17 at 14:47
  • *"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 Dec 13 '17 at 14:57
  • @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 – phflack Dec 13 '17 at 14:59
  • 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). – Tom Dec 13 '17 at 15:06
  • @Tom Understandable, I wanted to also link to a question about `city = city;`, but having trouble finding one (and also one on keeping the same style as the workplace) – phflack Dec 13 '17 at 15:26
  • A general question about `this` (which also generally covers `this.variable`, rather than `variable`) is that: https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class – Tom Dec 13 '17 at 15:29
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.

daniu
  • 14,137
  • 4
  • 32
  • 53
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

GuyKhmel
  • 505
  • 5
  • 15
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.

Crappy
  • 441
  • 3
  • 7
-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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215