Summary of this usage in Java

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/fzfengzhi/article/details/2174406

I know a lot of friends like me: in JAVA programs seem to see "This", and occasionally use it, but in the end "This" How to use, but'm not sure! A lot of people mention it, say "current object", but what is the current object, what is the current object, he himself is not clear. Now let's look at a small example to share with you the use of"This" in JAVA !

/**

* @author Fengzhi-neusoft

*

* This example is to illustrate the three usage of this!

*/

Package test;

Public class thistest {

Private int i=0;

First constructor: has an int type parameter

Thistest (int i ) {

this.i = I +1;//This represents the reference member variable I, not the function parameter I

System. out. println ("Int. constructor i-- this.i :" + I + "--" + this. i );

System. out. println ("I-1:" + ( i -1) + "this.i+1:" + ( this.i + 1));

The results from the two outputs amply demonstrate that I and this.i are not the same!

}

Second constructor: There is a string shape parameter

Thistest (String s) {

System. out. println ("String constructor:" +s);

}

Third constructor: There is an int type parameter and a string shape parameter

Thistest (int i,string s) {

This (s);//this calls the second constructor

This (i);

/* cannot be used here, because no other method can call the constructor, only the constructor will call him.

However, it is important to note that the constructor is called by the construction method, and it must be the first line, and the construction method can only be adjusted

Use one and only once constructors! */

this.i=i++;//this to refer to the member variables of the class

System. out. println ("Int constructor:" + i + "\ n" + "String constructor:" + s);

}

Public Thistest Increment () {

this.i+ +;

return this;//returns the current object that belongs to (Thistest)

}

Public Static void Main (string[] args) {

Thistest tt0=New thistest (10);

Thistest tt1=New thistest ("OK");

Thistest tt2=New thistest ("OK again!");

System. out. println (Tt0.increment (). Increment (). Increment (). i);

Tt0.increment () returns a Thistest object that is i++ on a tt0 basis,

It then returns the Thistest object on the basis of the object returned above i++!

}

}

Operation Result:

Int Constructor i--this.i:10--11

String Constructor:ok

String Constructor:ok again!

Int constructor:21

String Constructor:ok again!

14

Details of the comments have been written more clearly, here is not to repeat, just summed up, in fact, this is the main three ways to use:

1 . Returns the value of the object. Represents a reference to the current object!

2 . Reference member variable. To denote a member variable with a class, not a function parameter, note that the function arguments and member variables with the same name are differentiated! In fact, this is the first use of the special case, more commonly used, so that come out to emphasize.

3, call the constructor method. Used to reference the constructor in the constructor method that satisfies the specified parameter type (in fact, the constructor method). But there must be a lot of attention here: only one construction method can be referenced and must be at the beginning!

There is also note: This cannot be used in the static method! So even the definition of a static method is: There is no method for this! Although exaggerated, it is fully explained that this cannot be used in the static method!

Summary of this usage in Java

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.