Several uses of the This keyword in Java

Source: Internet
Author: User

    1. When you use this in a method when a member variable has the same name as a local variable, it represents a member variable in the same class as the method. (This is the current object itself)

such as : 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;//assigns the parameter value to the member variable, and the value of the member variable changes

System. out. println ("2, THIS.S =" + this. s);

}

Public Static void Main (string[] args) {

Hello x = new Hello ("helloworld!");

System. out. println ("s=" + x.s);//Verify the change of member variable value

}

}

The result is: s = helloworld!

1-THIS.S = Hello

2-THIS.S = helloworld!

s=helloworld!

In this example, in the constructor hello, the parameter s has the same name as the member variable s of class Hello, and if you operate directly on s then the parameter S is manipulated. To operate on the member variable s of class Hello, you should use this to refer to. The first line of the running result is to print directly to the parameter s passed in the constructor, and the second line is to print the member variable s, and the third line is to pass the parameter S value passed to the member variable s before printing, so the result is helloworld! and row four is the value of the member variable in the direct printing class in the main function , you can also verify the change in the value of the member variable.

2. You can also use this when passing yourself as a parameter. (This is passed as the current parameter)

class A

Public A () {

New B (this). Print ();//method to invoke B

}

Public void print () {

System. out. println ("Helloaa from a!");

}

}

class D =

A;

Public B (a A) {

this. A = A;

}

Public void print () {

A.print ();//method of invoking a

System. out. println ("Helloab from b!");

}

}

Public class Helloa {

Public Static void Main (string[] args) {

A AAA = new A ();

Aaa.print ();

b BBB = new B (AAA);

Bbb.print ();

}

}

The result is: HELLOAA from A!

Helloab from B!

HELLOAA from A!

HELLOAA from A!

Helloab from B!

In this example, object A's constructor, with new B (this), passes the object a itself as a parameter to the constructor of object B.

3. Sometimes, we use some internal classes and anonymous classes, such as event handling. When this is used in an anonymous class, this refers to the anonymous class or the inner class itself. If we want to use the methods and variables of the outer class, we should add the class name of the outer class. Such as:

Public class Hellob {

int i = 1;

Public Hellob () {

Thread thread = new thread () {

Public void Run () {

for (int j=0;j<20;j++) {

Hellob. this. run ();//method that calls the external class

Try {

Sleep (1000);

} catch (Interruptedexception IE) {

}

}

}

}; Notice there's a semicolon here.

Thread.Start ();

}

Public void Run () {

System. out. println ("i =" + i);

i++;

}

Public Static void Main (string[] args) throws Exception {

New Hellob ();

}

}

In the above example, thread is an anonymous class object, and in its definition, it uses the run function of the outer class. This is because the function has the same name and is not called directly. There are two ways to change the external run function to a name, but this approach is undesirable for a development-to-midway application. Then you can use the method in this example with the class name of the external class plus this to illustrate the way to invoke the external class of the run.

4. In constructors, this can be used to invoke constructors in the same class. Such as:

Public class thistest {

Thistest (String str) {

System. out. println (str);

}

Thistest () {

This ("This test succeeds!") ");

}

Public Static void Main (string[] args) {

Thistest thistest = new thistest ();

}

}

For a more precise explanation of this usage, another example is:

Public class thistest {

Private int age;

Private String str;

Thistest (String str) {

this. str=str;

System. out. println (str);

}

Thistest (String str,int age) {

This (str);

this. age=age;

System. out. println (age);

}

Public Static void Main (string[] args) {

Thistest thistest = new thistest ("This test succeeds", 25);

}

}

The result is: this test succeeds

25

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

5. This passes multiple parameters at the same time.

Public class TestClass {

int x;

int y;

Static void showtest (TestClass tc) {//Instantiation of Object

System. out. println (tc.x + "" + tc.y);

}

void Seeit () {

showtest (this);

}

Public Static void Main (string[] args) {

TestClass p = new TestClass ();

p.x = 9;

P.Y = 10;

P.seeit ();

}

}

The result is: 9 10

The Showtest (this) in the code, where this is the current instantiated P is passed to the Showtest () method, so it runs.

Several uses of the This keyword in Java (goto)

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.