Java this keyword

Source: Internet
Author: User

This is useful in the construction method

People.java

 Public classpeople{intLeg,hand;    String name; People (String s) {name=s;  This. Init ();//This can be omitted and will be This.init (); written as Init ();    }    voidinit () {Leg=2; Hand=2; SYSTEM.OUT.PRINTLN (Name+ "with" +hand+ "Hand" +leg+ "leg"); }     Public Static voidMain (String args[]) {people Boshi=NewPeople ("Bush");//when creating Boshi, this is the object in the constructor method Boshi    }} 

Using this in an instance method

class a{         int  x;          Static int y;          void f () {              this.x=100;              A.Y=200;         }}

The example method of Class A above appears in This,this, which represents the current object using F. So, "This.x" represents the current object's variable x, when the object calls method F, assigns 100 to the object's variable x, so when a pair of images is called, the instance member variable in the method is the instance member variable that the value is assigned to the object. While the static variable is shared with other objects. The above can also be written

class a{         int  x;          Static int y;          void f () {              int  x;               this. x=100;              Y=200;         }}

However, when the name of the instance member variable is the same as the name of the local variable, the class name "This" or "class name" precedes the member variable. Can not be omitted.

Java this keyword

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.