Java this keyword

Source: Internet
Author: User

The This keyword has three applications:
1.this calls the properties in this class, that is, the member variables in the class
2.this calling other methods in this class
3.this calls other construction methods in this class, which are placed on the first line of the constructor method when called

Take a look at the following code:
public string name;public void ShowName (string name) {    System.out.println (name);}

There is a member variable called name in the code above, and a method parameter called name. So does the ShowName method output the name of the parameter or the member variable name?

Test code:

Foo foo = new Foo (); foo.name = "Zhang San"; Foo.showname ("John Doe");

Operation Result:

John doe

The result of the run shows that the answer is the name of the output parameter, because the Java syntax takes the nearest rule when it gets the variable, so name represents the parameter name that is closest to it. If you want to output the member variable name, what do you do, change the name of the parameter? That's a lot of trouble to think of a variable name. Fortunately Java provides an object that the This keyword can refer to a class.


Change the code above to:

public string name;public void ShowName (string name) {    System.out.println ("I am the method parameter:" + name);    System.out.println ("I am the attribute of the class:" + THIS.name);}

Operation Result:

I am the method parameter: John Doe I am the property of the class: Zhang San

You can see that the This keyword represents the object that called the change method, which is equivalent to the object foo.

Let's look at one more example.

Public Foo Returnthis () {    return this;} public foo getinstance () {    return new Foo ();}

Test code:

Foo foo = new Foo (); Foo Foo2 = Foo.returnthis (); Foo Foo3 = Foo.getinstance (); System.out.println ("foo and Foo2 are the same objects?") --"+ (foo = = Foo2)); System.out.println ("foo and Foo3 are the same objects?") --"+ (foo = = Foo3));

Operation Result:

is foo and foo2 the same object? --Truefoo and Foo3 are the same objects? --False

This example makes it very clear that the "this keyword represents the object that called the Change method", in which return this returns the object that called the method, Foo, so Foo2 and Foo point to the same object. New Foo is the creation of a fresh Foo object. So Foo3 and Foo do not point to the same object.

 

Java this keyword

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.