About this use of

this as the default reference to an object in two ways:

  1. The reference constructor initializes the object constructor performs

  2. The reference object calls this method in the method

The biggest role is to allow access to another method class methods or properties in this class! (Static method can not be modified using this reference)

When a local variable with the same name as property, use this keyword!

  for example:

public class TestThis {
    String name = "black";
    public void PlayBall(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println (this.name + ", go play it!");
        this.name = name;
        System.out.println (name + ", go play it!");
    }

    public static void main(String[] args) {
        // here PlayBall method is non-static and need to call an object by new
        String name = "white";
        TestThis testThis = new TestThis();
        testThis.PlayBall(name);
    }

} 
// output
white
black
black, go play it!
White, go play it!  

 

Guess you like

Origin www.cnblogs.com/qingfengdream/p/11785873.html