java some usage of this keyword

1, when a member of the same name and local variables used in the method when the this, the class member variable represents the location in the method. ( The this is the current object itself)

E.g:

. 1  public  class the Hello {
 2    String STR = "hello" ;
 . 3  
. 4    public the Hello (String STR) {
 . 5      System.out.println ( "STR =" + STR);
 . 6      System.out.println ( "parameter assigned to members = variable before this.str "+ this .str);
 . 7      this ; .str STR = // parameter value assigned to the member variable, changing the value of a member variable 
8      after System.out.println (" member variables assigned to this parameter = .str "+ the this .str);
 . 9    }
 10  
. 11    public  static  void main (String [] args) {
 12 is      the Hello Hello = new newHello ( "Hello everyone" );
 13 is      System.out.println ( "STR =" + hello.str); // validate changing the value of the variable member 
14    }
 15 }

operation result:

In this example, the constructor Hello, the parameter str str class member variable with the same name Hello, then if str operate directly on the operating parameters are str. To the members of the class of Hello variable str operation should be carried out with reference to this. Str parameter value after the third row is first assigned to a member variable str pass over; the first line is the direct result of running the constructor of the passed parameter str print the results; the second line is printed on the member variable str print, so the result is Hello, everybody, and the fourth line is the value of a member variable of the main function of direct printing class, this usage is the development of the most common case, the entity classes DTO generated table will often see.

 

Guess you like

Origin www.cnblogs.com/1012hq/p/11202562.html