This is useful in the construction method
People.java
Public classpeople{intLeg,hand; String name; People (String s) {name=s; This. Init ();//This can be omitted and will be This.init (); written as Init (); } voidinit () {Leg=2; Hand=2; SYSTEM.OUT.PRINTLN (Name+ "with" +hand+ "Hand" +leg+ "leg"); } Public Static voidMain (String args[]) {people Boshi=NewPeople ("Bush");//when creating Boshi, this is the object in the constructor method Boshi }}
Using this in an instance method
class a{ int x; Static int y; void f () { this.x=100; A.Y=200; }}
The example method of Class A above appears in This,this, which represents the current object using F. So, "This.x" represents the current object's variable x, when the object calls method F, assigns 100 to the object's variable x, so when a pair of images is called, the instance member variable in the method is the instance member variable that the value is assigned to the object. While the static variable is shared with other objects. The above can also be written
class a{ int x; Static int y; void f () { int x; this. x=100; Y=200; }}
However, when the name of the instance member variable is the same as the name of the local variable, the class name "This" or "class name" precedes the member variable. Can not be omitted.
Java this keyword