-
The This keyword appears in the constructor method of the class, and represents the object that is created by using the constructor method.
-
public class people { /** * @author Shepherd Queen Berg * @param args * date:2015-10-14 */ private int age; private string name; people (String name) { This.name = name; this.printinfo (); // can also omit This } public void printinfo () { age = 21; //can also be used here this.age system.out.println ( name + "Age is: " + age ); } public static void main (String[] args) { &nBsp; people p = new people ("Queen of Sheep"); //this is the object in the constructor method p } }
3. Using this in an instance method, an instance method can only be called by an object and cannot be invoked with a class name. When this is present in an instance method, this represents the current object that is calling the method.
It is also important to note that when the name of the instance member variable is the same as the name of the local variable, the "this." or "class name" precedes the member variable. Can not be omitted.
public class demo { public int x = 100; public static int y; public void a () { int x =10; demo.y = 20; //The This keyword cannot be omitted when the instance member variable is the same as the name of the local variable system.out.println ("x = " +x + "\t this.x = " + this.x"); this. B (); // the current object is called b it can also be said that object D called method A, and then called the method b this keyword can also be omitted at this time c (); } public void b () { system.out.println ("Hello , b method ! " );     }&NBSp; public static void c () { System.out.println ("hello , c method !"); } public static void main (String[] args) { Demo d = New demo (); d.a (); }}
Note: The This keyword cannot appear in a class method because the class method can be called directly through the class name, at which point there may not be any objects created.
-------a deeper understanding of the This keyword: several uses of the This keyword in Java
The This keyword in Java