Java basics-usage of this in Java

Source: Internet
Author: User

1. this refers to the current object itself.
This reference should be added when the variables or functions of the object must be explicitly specified in a class. In the following example:

Public class {

String s = "Hello ";

Public A (String s ){
System. out. println ("s =" + s );
System. out. println ("1-> this. s =" + this. s );
This. s = s;
System. out. println ("2-> this. s =" + this. s );
}

Public static void main (String [] args ){
New A ("HelloWorld! ");
}
}

Running result:

S = HelloWorld!
1-> this. s = Hello
2-> this. s = HelloWorld!

In this example, in constructor A, the parameter s has the same name as the variable s in Class A. In this case, if the parameter s is directly operated on, the parameter s is operated. To operate the variable s of Class A, use this for reference. The first line of the running result is to directly print the result of the parameter s; the second line is the result of the operation before and after the variable s of object.

2. Pass this as a parameter
When you want to pass yourself as a parameter to another object, you can also use this. For example:

Public class {
Public (){
New B (this). print ();
}

Public void print (){
System. out. println ("Hello from! ");
}
}

Public class B {
A;
Public B (A ){
This. a =;
}

Public void print (){
A. print ();
System. out. println ("Hello from B! ");
}
}

Running result:
Hello from!
Hello from B!

In this example, the constructor of object A uses new B (this) to pass object A as A parameter to the constructor of object B.

3. Pay attention to this in the anonymous class and internal class.
Sometimes, some internal and anonymous classes are used. When this is used in an anonymous class, this refers to the anonymous class or internal class itself. If we want to use external class methods and variables, we should add the Class Name of the external class. For example:

Public class {
Int I = 1;

Public (){
Thread thread = new Thread (){
Public void run (){
For (;;){
A. this. run ();
Try {
Sleep (1000 );
} Catch (InterruptedException ie ){
}
}
}
};
Thread. start ();
}

Public void run (){
System. out. println ("I =" + I );
I ++;
}

Public static void main (String [] args) throws Exception {
New ();
}

}

In the above example, thread is an anonymous class object. In its definition, its run function uses the run function of the external class. In this case, the function cannot be called directly due to the same name. At this time, there are two ways, one is to change the external run function name, but this method is not available for an application that has been developed to the middle of the process. Then we can use the method in this example to add this reference to the Class Name of the external class to indicate that the method run of the external class is called.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.