1.this concept
(Reference: http://www.cnblogs.com/xdp-gacl/p/3636071.html)
This is a reference to its own object, and its memory is parsed as such. Suppose we have a new object in the heap memory, and in this object we assume that there is a reference this,this pointing to the object itself, so this is this, what is the name of this new object, we don't know, it doesn't matter, because it doesn't affect the existence of this object in memory, As long as the object exists in memory, he must have a reference to this.
Application of 2.this in methods--distinguishing member variables from local variables
When a method is running, a JVM will open up a space in the stack memory for this method to run in this memory space.
If a variable is accessed in a method that only has a member variable, the Java compiler adds the This keyword before the variable. If there is a member variable with the same name and a local variable, the default inside the method is to access the data of the local variable, you can specify the data that accesses the member variable through the This keyword, for example, by separating the member variable i and the local variable i.
3.this application in constructors--calling other constructors and distinguishing member variables from local variables
The This keyword must be in the first statement in the constructor when calling other constructors.
The This keyword cannot be called in a constructor because it is a dead loop.
Public classconstructfuction { Publicconstructfuction () {} PublicConstructfuction (intID) { This();//call no parameter amount construction methodSystem.out.println (ID); } PublicConstructfuction (intIdintAge ) { This(ID);//Use this to call other constructorsSystem.out.println (age); } PublicConstructfuction (intIdintage,string name) { This(Id,age); SYSTEM.OUT.PRINTLN (name); }}
4.this as the return value of the method-returns the object itself
class returnobjectself{ public String name= "Hello World"; Public returnobjectself returnobject () { returnthis; }}
5.this use in internal classes
This represents the inner class in the inner class and represents the outer class in the outer class.
The inner class calls the member variable of the outer class, either directly or with the class name. This member variable.
When an inner class calls a variable of an external class, it distinguishes between the member variable and the local variable, when there is a conflict, the local variable is called directly, and the member variable is used with the class name. This member variable
Public classInnerclassthis { PublicString outer= "Hello outer"; //Inner class Public classInnerclasstest { PublicString inner= "Hello inner"; Public voidShowinfo () {System.out.print ( This. Inner);//Inner class calls internal class member variableSystem.out.print (outer);//the inner class calls the member variable of the outer classSystem.out.print (innerclassthis. This. Outer); } } Public voidPartclass () {String outer= "Hello Outer part"; classPartclasstest { Public voidShowinfo () {System.out.println (outer); //Inner class calls external class local variablesSystem.out.print (innerclassthis. This. Outer);//Inner class calls external class member variable}} partclasstest pct=Newpartclasstest (); Pct.showinfo (); } }
[Application of this in Java Foundation]java