Where to use "this" in java?

As far as I know, this is used in the following situations:

  • this is the keyword used when we want to refer to an instance variable with the same name as the local variable.
  • Call one constructor for another class.
  • Pass an instance of the class as an argument to the method.
  • Access to external class variables.

But I looked at the project code, where they are used in getters, for example:

 class a { int time; int getValue() { return this.time * 5.; } } 

As far as I know, each object has its own copy of instance variables and methods, so returning this path makes sense. Please clarify.

Stackoverfow Objective: When Should I Use "his" in a Class?

+6
source share
4 answers

Many people (I am one of them) use this keywords, even if this is not explicitly required.

  • It becomes clearer for some people to put this before what belongs to one class, the same logic applies to super .
  • Some people always use this reflex for better autocompletion (a pop-up window appears automatically, a lazy programmer) in some IDEs.

These points are mostly opinion-based and not a big deal.

For other applications, as you noted, it can be used to pass a parameter to another constructor, pass an instance of a class or an individual element when you have two variables with the same name.

However, IMO, the easiest way is simply not to have multiple variables with the same name.

+9
source

If you have a method similar to the following:

 public void setFoo(Foo foo) { this.foo = foo; } 

Using this is a must. Otherwise, it assigns the argument foo to itself, and does not assign it to the instance variable.

But this does not mean that the only situation in which you can use this . The following code is strictly equivalent:

 public void setFoo(Foo newFoo) { this.foo = newFoo; } 

Although in this case you can write

 public void setFoo(Foo newFoo) { foo = newFoo; } 

because this no longer required . This does not make it illegal .

So,

 int getValue() { return time * 5; } 

and

 int getValue() { return this.time * 5; } 

strictly equivalent.

+6
source

The keyword 'this' is used to refer to the currently used link object. It is simply used as syntactic sugar to simplify the performance of certain tasks, such as calling constructors in the same class, accessing fields in the same class, parameter types.

Here is an example of two different programming styles. Both do the same, but the first example uses 'this' to explicitly call the constructor again.

  public class Foo { public Foo() { this("Hello"); } //Explicit constructor call public Foo(String string) { System.out.print(string); } } public class Bar { public Bar() { new Bar("Hello"); } public Bar(String string) { System.out.print(string); } } 
+1
source

The keyword 'this' is often used from anonymous classes to refer to fields in the containing class. For instance:

 public class App { public String myString = "This is Java"; public App() { JButton button = new JButton("Test"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { System.out.println(App.this.myString); // <-- App.this gives access to public fields in App }}); } } 
+1
source

Source: https://habr.com/ru/post/988036/


All Articles