This is a keyword in Java that represents an object. This can appear in instance methods and constructors, but not in class methods.
Use this in the constructor
When the this keyword appears in the constructor of a class, it represents the object created using that constructor.
For example:
publicclassPeople{intleg,hand;Stringname;People(Strings){name=s;this.init();//This. can be omitted, that is, written as init();}voidinit(){leg=2;hand=2;System .out.println(name+have+hand+hand+1eg+leg);}publicstaticvoidmain(Stringargs[]){Peopleboshi=newPeople(Bush);}}
Using this in instance methods
Instance methods can only be called through the object, not through the class name. When the this keyword appears in an instance method, it represents the current object that is calling the method.
Instance methods can operate member variables of a class. When instance member variables appear in an instance method, the general format is:
this.member variable;
When static member variables appear in instance methods, the general format is:
Class name.Member variable;
For example:
classA{intx;staticinty;voida(){this.x=10;Ay=20;}}
This appears in the instance method a in class A. This represents the current object using a, so "this.x" represents the variable x of the current object. When the object calls method a, 10 is assigned to the variable of the object. x. When an object calls a method, the instance member variables in the method refer to the instance member variables assigned to the object, and static variables are shared with other objects. Therefore, under normal circumstances, you can omit "this." in front of the instance member variable name and "class name." in front of the static variable.
For example:
classA{intx;staticinty;voida(){x=10;//Omit this.y=20;//Omit the class name.}}
Notice:
When the name of the instance member variable is the same as the name of the local variable, the "this." or "class name." in front of the member variable cannot be omitted.
Instance methods of a class can call other methods of the class. The general format for calling instance methods is:
this.method;
The general format for class method calls is:
classname.method;
For example:
classB{voidb(){this.c();Bd();}voidc(){System.out.println(hi);}staticvoidd(){System.out.println(ok);}}
This appears in method b in class B, and this represents the current object that calls method b, so this.c() in the method body of method b is the current object calling method c, that is, calling the method on a certain object In the process of b, method c is called again. Since this logical relationship is very clear, when one instance method calls another method, you can omit "this." or "class name." in front of the method name.
For example:
classB{voidb(){c();//Omit this.d();//Omit the class name.}voidc(){System.out.println(hi);}staticvoidd(){System.out.println( ok);}}
Notice:
This cannot appear in class methods because class methods can be called directly through the class name, and no object may have been born yet.