Java - 'this' operator [closed]

喜欢而已 提交于 2020-03-03 09:12:56

问题


I have a question about the this operator in Java. In the case where a programmer would write a code like this:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}

Would it be better to avoid shadowing effect and go with this:

private int counter;

public Incrementor(int startValue){
counter = startValue;
}

Wouldn't the this operator make the this operator obsolete in future programming execrises?


回答1:


The "this" keyword is very useful. Your example is clear, but imagine the situation in which you have to pass a self reference to another method of an external object.




回答2:


this is a reference to the current object. In your example this.counter means the counter variable defined in the class and the method argument counter is a local variable for the constructor.

The main idea here is to explicitly mention that you are using the variable of the current object. So it improves readability and understandability of your code.

Also you should know that this is not an operator ... it's a reference to the current object.




回答3:


this is used in more scenarios:

  • it allows differentiating local variables from fields
  • it allows passing the object as argument to methods foo.add(this)
  • it allows the current object to be referred from inner classes: Outerclass.this
  • it is used to invoke other constructors of the same class: this()



回答4:


It's a matter of preference, but most programmers prefer the convenience of using the same name. Is it difficult to remember to refer to the class variables with this ?




回答5:


There is no shadowing in your first example. this is a very clear way in order to refer to class variables.




回答6:


The way I see it, this is simply a matter of good programming practices when you are doing it for class variables.

A code like:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}

vs.

private int counter;

public Incrementor(int startValue){
counter = startValue;
}

Extrapolate it to 10 lines, and the first one is much more readable. Moreover, it also makes refactoring code easier.

Otherwise, 'this' has a special significance. The 'this' keyword refers to the current object inside a class and falls in line with object-oriented principles. It can be extremely helpful and meaningful in case of getters and setters.

Added:

Once you start working with inheritance, you will notice another keyword called 'super' that can significantly differentiate between attributes that are inherited and ones that are local inside a class.




回答7:


In small methods, it is often more clear to give arguments the same name as the members they will be assigned to. If you are working with a larger method, this could become confusing. But then you need to evaluate whether the right decision is to change the name of the argument(s) or to decompose the method into sub-functions.




回答8:


I would use the first one. It makes the code more readable. Also you should name the method setCounter() as this also lends to readability.

Another unrelated point is to stick with the Java Coding conventions for naming methods as outlined in the Methods section here.

Edit: That wasn't a method was it. Oops!




回答9:


Sometimes, you need to pass the object itself. That's when you use this.



来源:https://stackoverflow.com/questions/7869257/java-this-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!