Summary of this usage in Java

Source: Internet
Author: User

The Java keyword This can only be used in a method method body. When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer. Therefore, this can only be used in non-static methods in the class, static methods and static code blocks must never appear in this, which is explained in the article "Java keyword static, final use summary". And this is only associated with a particular object, not with a class, and different objects of the same class have different this.

Package test;

Public class Thistest {

Private int i=0;

//First constructor: has an int type parameter

Thistest (int i) {

        this .i=i+1; At this point 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));

//From the results of the two outputs fully proves 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: has an int type parameter and a string shape parameter

Thistest (int i,string s) {

This (s); This invokes a second constructor

//this (i);

This . i=i++; This to reference the member variable 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 (ten);

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 based on the tt0 i + +,

//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. Represents a reference to the current object!

2, the use of class member variables, rather than function parameters, note that the function parameters 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, used in the construction method to reference the constructor 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: Thiscannot 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!

indicate under what circumstances this is required:
First, the use of this call another constructor method, with the hair is this (parameter list), this is only in the construction method of the class, other places can not be used.
Second, in the case of a function parameter or local variable in a function with the same name as a member variable, the member variable is masked, and the member variable needs to be referenced by the "this. Member variable name" method to access the member variable. Of course, in the absence of the same name, you can directly use the name of the member variable, instead of this, use is not wrong, hehe.
Third, in the function, it is necessary to refer to the current object of the class that the letter belongs to, directly with this.

In fact, these usage summary is from the "This is a pointer to the object itself" this sentence of a deeper understanding of the word, rote or easy to forget and easy to make mistakes, to understand!

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.