[Application of this in Java Foundation]java

Source: Internet
Author: User

1.this concept

(Reference: http://www.cnblogs.com/xdp-gacl/p/3636071.html)

This is a reference to its own object, and its memory is parsed as such. Suppose we have a new object in the heap memory, and in this object we assume that there is a reference this,this pointing to the object itself, so this is this, what is the name of this new object, we don't know, it doesn't matter, because it doesn't affect the existence of this object in memory, As long as the object exists in memory, he must have a reference to this.

Application of 2.this in methods--distinguishing member variables from local variables

When a method is running, a JVM will open up a space in the stack memory for this method to run in this memory space.

If a variable is accessed in a method that only has a member variable, the Java compiler adds the This keyword before the variable. If there is a member variable with the same name and a local variable, the default inside the method is to access the data of the local variable, you can specify the data that accesses the member variable through the This keyword, for example, by separating the member variable i and the local variable i.

3.this application in constructors--calling other constructors and distinguishing member variables from local variables

The This keyword must be in the first statement in the constructor when calling other constructors.
The This keyword cannot be called in a constructor because it is a dead loop.

 Public classconstructfuction { Publicconstructfuction () {} PublicConstructfuction (intID) { This();//call no parameter amount construction methodSystem.out.println (ID); }         PublicConstructfuction (intIdintAge ) {         This(ID);//Use this to call other constructorsSystem.out.println (age); }         PublicConstructfuction (intIdintage,string name) {         This(Id,age);    SYSTEM.OUT.PRINTLN (name); }}

4.this as the return value of the method-returns the object itself

class returnobjectself{    public String name= "Hello World";       Public returnobjectself returnobject ()    {        returnthis;    }}

5.this use in internal classes

This represents the inner class in the inner class and represents the outer class in the outer class.

The inner class calls the member variable of the outer class, either directly or with the class name. This member variable.

When an inner class calls a variable of an external class, it distinguishes between the member variable and the local variable, when there is a conflict, the local variable is called directly, and the member variable is used with the class name. This member variable

 Public classInnerclassthis { PublicString outer= "Hello outer"; //Inner class     Public classInnerclasstest { PublicString inner= "Hello inner";  Public voidShowinfo () {System.out.print ( This. Inner);//Inner class calls internal class member variableSystem.out.print (outer);//the inner class calls the member variable of the outer classSystem.out.print (innerclassthis. This. Outer); }    }         Public voidPartclass () {String outer= "Hello Outer part"; classPartclasstest { Public voidShowinfo () {System.out.println (outer); //Inner class calls external class local variablesSystem.out.print (innerclassthis. This. Outer);//Inner class calls external class member variable}} partclasstest pct=Newpartclasstest ();            Pct.showinfo (); }    }

[Application of this in Java Foundation]java

Related Article

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.