The use of this in Java is as follows: This.name=name

Source: Internet
Author: User

Package Com.chensi;/** This is to understand the THIS.name = name. * @author ZHL **/ Public classTHISTESTZHL {Private inti =0; PrivateString s ="ZHL"; //first ConstructorTHISTESTZHL (inti) {System. out. println ("first Constructor"); //This time of this. I represents the member variable of i is 0, and then this. i = i; the member variable class is assigned by local variable.System. out. println ("this is this. i--"+( This. i)); System. out. println ("this is i-- ."+i); System. out. println ("----------");  This. i =i; System. out. println ("this is i-- ."+i+"\ n"+"this is this. i--"+( This. i)); System. out. println ("*********"); System. out. println ("this.i----"+( This. i)); }    //a second constructorThistestzhl (String s) {System. out. println ("a second constructor"); System. out. println ("s--"+s); System. out. println ("THIS.S---"+( This. s));  This. S =s; System. out. println ("---------"); System. out. println ("String Constructor"+s); System. out. println ("String Constructor this.s--"+( This. s)); }    //Third constructor, a constructor with two parametersTHISTESTZHL (intI, String s) {        //This (i);         This(s); System. out. println ("a third constructor"); }         PublicThistestzhl Increment () {System. out. println ("entrance"); System. out. println ("Entrance I---"+i); System. out. println ("Entrance THIS.I---"+( This. i));  This. i++; System. out. println ("Export I---"+i); System. out. println ("Export THIS.I---"+( This. i)); return  This; }                 Public Static voidMain (string[] args) {THISTESTZHL tto=NewTHISTESTZHL (Ten); //THISTESTZHL Tto2 = new Thistestzhl ("Dou Dou"); //THISTESTZHL Tto3 = new Thistestzhl ("Chensi");System. out. println (Tto.increment (). Increment (). Increment (). i); //tto.increment ();    }                            /*this: Through this you can call the constructor method of this class, member variable, member method * This () Call constructor method * this. variable name call member variable * this. Method name Call member Method*/        /** Details of the comments have been written more clearly, here is not to repeat, just summed up, in fact, this mainly to three kinds of usage: 1, represents the current object reference! 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!                Indicates under what circumstances it is necessary to use this: first, call another construction method through this, with the hair is this (argument list), this is only in the constructor of the class, other places can not be used in this way.                            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! */    }

Results:

First constructor
This is this. i--0
This is i--10.
----------
This is i--10.
This is this. I--10
*********
THIS.I----10
Entrance
Entrance I---10
Entrance THIS.I---10
Exit I---11
Export THIS.I---11
Entrance
Entrance I---11
Entrance THIS.I---11
Exit I---12
Export THIS.I---12
Entrance
Entrance I---12
Entrance THIS.I---12
Exit I---13
Export THIS.I---13
13

The use of this in Java is as follows: This.name=name

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.