The Java keyword This can only be used in the method body. When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer. Therefore, this can only be used in non-static methods in a class, static methods and static code blocks must never appear in this, and this is only associated with a particular object, not with a class, and different objects of the same class have different this.
Package test;
Public class thistest
{
Private int i=0;
//First constructor: has an int type parameter
Thistest (int i)
{
this .i=i+1; At this point this represents the reference member variable I, not the function parameter I
system. out .println ( "Int constructor i--this.i: "+i+ "--" + this i);
System. out . println ("i-1:"+ (i-1) +"this.i+1:"+ (this. ) I+ 1));
//From the results of the two outputs fully proves that I and this.i are not the same!
}
//Second constructor: There is a string shape parameter
Thistest (String s)
{
System. out . println ("String constructor:"+s);
}
//Third constructor: has an int type parameter and a string shape parameter
Thistest (int i,string s)
{
This (s); This invokes a second constructor
//this (i);
This . i=i++; This to reference the member variable of the class
System. out . println ("Int constructor:"+i+"/n"+"String constructor:"+s);
}
Public thistest increment ()
{
This . i+ +;
return This ; //Returns the current object that belongs to (thistest)
}
Public Static void Main (string[] args)
{
Thistest tt0=New Thistest (ten);
Thistest tt1=New thistest ("OK");
Thistest tt2=New thistest ("OK again!" );
System. out . println (Tt0.increment (). Increment (). Increment (). I);
//tt0.increment () returns a thistest object that is based on the tt0 i + +,
//Then returns the Thistest object on the basis of the object returned above i++ !
}
}
Operation Result:
Int Constructor i--this.i:10--11
String Constructor:ok
String Constructor:ok again!
Int constructor:21
String Constructor:ok again!
14
Details of the comments have been written more clearly, here is not to repeat, just summed up, in fact, this is the main three kinds of usage:
1. Represents a reference to the current object!
2, the use of class member variables, rather than function parameters, note that the function parameters and member variables with the same name when the distinction! In fact, this is the first use of the special case, more commonly used, so take it out to emphasize.
3, used in the construction method to reference the constructor that satisfies the specified parameter type (in fact, the constructor method). But there must be a lot of attention here: only one construction method can be referenced and must be at the beginning!
There is also note: Thiscannot be used in the static method! So even the definition of a static method is: There is no method for this! Although exaggerated, it is fully explained that this cannot be used in the static method!
indicate under what circumstances this is required:
First, the use of this call another constructor method, with the hair is this (parameter list), this is only in the construction method of the class, other places can not be used.
Second, in the case of a function parameter or local variable in a function with the same name as a member variable, the member variable is masked, and the member variable needs to be referenced by the "this. Member variable name" method to access the member variable. Of course, in the absence of the same name, you can use the name of the member variable directly, instead of this, it is not a mistake.
Third, in a function, you need to refer to the current object of the class to which the function belongs, use this directly.
In fact, these usage summaries come from a deeper understanding of the phrase "This is a pointer to the object itself"!
Summary of this usage in Java