When is this the only way to go?

The following code runs for both var = putVar; & Amp; this.var = putVar;

I understand: "this" is used to determine that - "put this value only for" my "object." When both work, why do people usually use this in setters?

code:

public class PlayingWithObjects { public static void main(String[] args) { SomeClass classObj = new SomeClass(10); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj.setVar(20); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj = new SomeClass(30); System.out.println("classObj.getVar: " + classObj.getVar() ); } } class SomeClass { private int var; public SomeClass(int putVar) { var = putVar; } public int getVar() { return var; } public void setVar(int putVar) { // var = putVar; // also works this.var = putVar; } } 

Do I understand "this" correctly? Where "this" is used and cannot be replaced. Please post the code.

0
source share
5 answers

Since people like to use the same variable name for a method parameter and an instance variable - in this case you need to distinguish this .

 public void setX(int x) { this.x = x; } 
+7
source

You can use this if you use the same method argument argument as the field, but it can be avoided if you just don't use the same name.

Not using the same name is more common practice to avoid confusion and shadowing. Therefore, any reference to this in the setter can be replaced by a better naming standard: inParameter , for example.

 public void setX(int inX) { x = inX; } 

Another use of this would be to explicitly call the constructor . This is a form that cannot be replaced by a simpler naming convention:

 public class Foo { private String name; public Foo() { this(""); } public Foo(String inName) { name = inName; } } 

There may also be a case in which you want to return the instance you are working with. This is also what this allows:

 return this; 
+2
source

There are two cases that I know of, other than the ktm case mentioned (which, I think, is obvious, and you already knew):

  • Just to clearly indicate that they are related to a member of the current object.

     void foo(int x) { this.y = x; // No mistaking that y belongs to the object } 
  • If you are in an anonymous inner class inside another object (for example: class ClassName), you can use ClassName.this to get an instance of the surrounding object. The reason for this (no pun intended) is that inside the inner class this will refer to the inner class.

     SomeInnerClass myObj = new SomeInnerClass() { void bar() { this.y = 0; // this refers to the SomeInnerClass object OuterClass.this.y = 0; // OuterClass.this refers to the enclosing class OuterClass object } }; 
+1
source

As ktm is mentioned, setters tend to use the same name as the field for the parameter. In this case, the parameter obscures the field name, therefore

 public void setX(int x) { x = x; } 

just set the parameter by itself, and not set the field to the parameter.

0
source

If the class has an instance variable with some name (for example, myVar ), and the method has a LOCAL variable with the same name ( myVar ), then any reference to the variable will refer to the LOCAL variable, In such cases, if we want to specify an instance variable class, we have to say this.myVar . As mentioned earlier, this is especially useful when we want to have setters in which the parameter name matches the instance variable that it sets:

 public void setMyVar(int myVar) { this.myVar = myVar; // If we said myVar = myVar;, we'd just set the local // variable to itself, which would NOT be what we want. } 
0
source

All Articles