Usage of this in Java

Source: Internet
Author: User

This in Java is everywhere and used in many ways. The following points are available:

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 Hello {

String S = "hello ";

Public Hello (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 ){
Hello x = new Hello ("helloworld! ");
}
}

 

Running result:

S = helloworld!
1-> This. S = Hello
2-> This. S = helloworld!

 

In this example, In the constructor hello, the parameter S has the same name as the variable s in the Hello class. In this case, if the parameter S is operated directly, the parameter S is operated. To operate the member variable s of the Hello class, use this to reference it. Running resultThe first line is to print the result of the parameter S passed in the constructor; The second row prints the member variable S.;The third line is to first print the parameter S value assigned to the member variable S.So the result is helloworld!

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 (){
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, we use some internal and anonymous classes, such as event processing. 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 ){}

}
}
}; // Note the following;
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.

 

4. In constructor, you can use this to call other constructor in the same class, as shown in

Public class flower {
Flower (INT petals ){}
Flower (string SS ){}
Flower (INT petals, Sting SS ){
// Petals ++; the statement that calls another constructor must be at the starting position
This (petals );
// This (SS); will generate an error because only one constructor can be called in one constructor.
}
}

It is worth noting that: 1: When constructing and calling another constructor, the call action must be placed at the starting position. 2: The constructor cannot be called in any function other than the constructor. 3: Only one constructor can be called within a constructor.

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.