Three ways to use this in Java
Call Properties
(1) This can call any member variable in this class
Calling method (can be omitted)
(2) This invokes the member method in this class (there is no way to call through this in the Main method)
Call construction method
(3) This call constructor method can only invoke another constructor method in this constructor method
(4) This call construction method must be written in the first row
eg
1 Public classThisdemo {2 Private intID;3 PrivateString name;4 PublicThisdemo () {//(1) This can call any member variable in this class5Name= "Cosmosray";6id=110;7 //this.shoinfo (); ==shoinfo ();8 This. Shoinfo ();//(2) This invokes the member method in this class (there is no way to call through this in the Main method)9 }Ten PublicThisdemo (String name) { One This. name=name; A } - PublicThisdemo (String name,intID) { - //This (); //this invokes the non-parametric construction method in this class the //(3) This call constructor method can only invoke another constructor method in this constructor method - //(4) This call construction method must be written in the first row - This(name);//this invokes the method of the argument construction in this class - } + Public voidShoinfo () { -System.out.println ( This. id+ "" + This. Name); + } A Public Static voidMain (string[] args) {//entry of the program, static at NewThisdemo ("John Doe");//Anonymous Objects -Thisdemo demo;//define a variable name -demo=NewThisdemo ();//Create an Object - - } -}
Three ways to use this in Java