Usage of this 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.

 Packagetest; Public classThistest {Private intI=0; //first constructor: has an int type parameterThistest (inti) {       This. i=i+1;//This represents the reference member variable I, and not the function parameter ISystem.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 parameterThistest (String s) {System.out.println ("String constructor:" +1); }    //Third constructor: There is an int type parameter and a string shape parameterThistest (inti,string s) {       This(s);//This invokes a second constructor//This (i);             This. i=i++;//this to reference the member variable of the classSystem.out.println ("Int constructor:" +i+ "/n" + "String constructor:" +s); }    Publicthistest Increment () { This. i++; return  This;//returns the current object that belongs to (thistest)    }    Public Static voidMain (string[] args) {thistest tt0=NewThistest (10); Thistest TT1=NewThistest ("OK"); Thistest TT2=NewThistest ("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++! }} Run Result: Int constructor i-- This. i:10--11String constructor:okstring Constructor:ok again!Int Constructor:21stString 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: 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!

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!

Usage of this 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.