Three ways to use this in Java

Source: Internet
Author: User

Three ways to use this in Java

Call Properties

(1) This can call any member variable in this class

Calling method (can be omitted)

(2) This invokes the member method in this class (there is no way to call through this in the Main method)

Call construction method

(3) This call constructor method can only invoke another constructor method in this constructor method
(4) This call construction method must be written in the first row

eg

1  Public classThisdemo {2     Private intID;3     PrivateString name;4      PublicThisdemo () {//(1) This can call any member variable in this class5Name= "Cosmosray";6id=110;7         //this.shoinfo (); ==shoinfo ();8          This. Shoinfo ();//(2) This invokes the member method in this class (there is no way to call through this in the Main method)9     }Ten      PublicThisdemo (String name) { One          This. name=name; A     } -      PublicThisdemo (String name,intID) { - //This (); //this invokes the non-parametric construction method in this class the         //(3) This call constructor method can only invoke another constructor method in this constructor method -         //(4) This call construction method must be written in the first row -          This(name);//this invokes the method of the argument construction in this class -     } +      Public voidShoinfo () { -System.out.println ( This. id+ "" + This. Name); +     } A      Public Static voidMain (string[] args) {//entry of the program, static at         NewThisdemo ("John Doe");//Anonymous Objects -Thisdemo demo;//define a variable name -demo=NewThisdemo ();//Create an Object -          -     } -}

Three ways to use 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.