Java the use of the This keyword

Source: Internet
Author: User

The This keyword in Java refers to the current object, which can be used to invoke the current object's member variables directly or directly with the current object's member method, this is our common scenario, then there is no other situation!

This can also be used in the construction of the parameterless method directly with a parenthesis, thus invoking the construction method of the parameter, so that when we re new to another object, we can no longer be initialized by the construction method of a parameter, directly through the construction method without parameters immediately.

The code is as follows:

 Public classStudent {PrivateString name = "Zhang San"; Private intsum = 0;  PublicStudent () { This("Harry");//calling a constructed method of a parameter    }         PublicStudent (String name) { This. name = name;//call the name in the member variable to distinguish the name in the method    }         Public voidsay () {System.out.println ( This. Name); }        /*** Recursive summation *@paramNum*/     Public voidSumintnum) {         This. sum + = num--; if(0 <num)        {sum (num); } Else{System.out.println ("sum =" +sum);  This. Say ();//Call member Method say ()        }    }    }

Java the use of the 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.