Talk about the use of this in Java

  • 2020-04-01 02:47:39
  • OfStack

1.   This refers to the current object itself.
      This reference should be added to a class to specify that it USES the object's own variables or functions. As in the following example:


public class A { 
  String s = "Hello";    
  public A(String s) { 
    System.out.println("s = " + s); 
    System.out.println("1 -> this.s = " + this.s); 
    this.s = s; 
    System.out.println("2 -> this.s = " + this.s); 
  } 

  public static void main(String[] args) { 
    new A("HelloWorld!"); 
  } 
} 

Running result:  


S = HelloWorld!
1 - > Enclosing the s = Hello
2 - > Enclosing the s = HelloWorld!

      In this example, in constructor A, the parameter s has the same name as the variable s of class A, so if you directly manipulate s, you are manipulating parameter s. To manipulate the variable s of class A, you should use this as A reference. The first line of the result is to print directly to parameter s; The next two lines are printed before and after the operation on the variable s of object A.

2.   Pass this as an argument

      You can also use this when you want to pass yourself as an argument to another object. Such as:


public class A { 
  public A() { 
    new B(this).print(); 
  } 

  public void print() { 
    System.out.println("Hello from A!"); 
  } 
} 
public class B { 
  A a; 
  public B(A a) { 
    this.a = a; 
  } 

  public void print() { 
    a.print(); 
    System.out.println("Hello from B!"); 
  } 
} 

Operation results:
Hello from A!
Hello from B!

      In this example, object A's constructor passes object A itself to object B's constructor with new B(this).

3.   Notice this in the anonymous and inner classes.

      Sometimes we use inner and anonymous classes. When used in an anonymous class, this refers to the anonymous class or the inner class itself. If we want to use the methods and variables of the external class, we should add the class name of the external class. Here's an example:


public class A { 
  int i = 1; 
  public A() { 
    Thread thread = new Thread() { 
      public void run() { 
        for(;;) { 
          A.this.run(); 
          try { 
            sleep(1000); 
          } catch(InterruptedException ie) { 
          } 
        } 
      } 
    }; 
    thread.start(); 
  }   
  public void run() { 
    System.out.println("i = " + i); 
    i++; 
  } 
  public static void main(String[] args) throws Exception { 
    new A(); 
  } 
} 

      In the above example, thread is an anonymous class object that USES the run function of an external class in its run function in its definition. Because the function is the same name, calling it directly won't work. There are two ways to do this, one is to change the name of the external run function, but this is not an option for an application in the middle of development. Then you can use the method in this example to indicate that you are calling the method run of the external class with the class name of the external class plus this reference.


Related articles: