Three methods in a Class 1 global Method 2 member Method 3 construction method

Source: Internet
Author: User

  

 Public classtest{Private  intAge//This is a member variable PublicTest (intAge) {//This is the construction method This. Age =Age ;} Public voidSetage (intAge) {//This is the member method This. Age =Age ;} Public Static intGetage () {//This is the global method, with static, the member method becomes the global methodreturn  This. Age;}}

Member methods have nothing to do with constructing methods, except that the member method must be accessed with the instantiation object of the class, while the global method is accessed directly by the class, and the constructor is initialized when the object is instantiated.

Explain the construction method first:

Test T = new Test (34);

Here the new Test (34) parentheses represent the construction method, the general construction method is the default, you can not write, if the above construction method does not write, here should instantiate the object:

Test T = new Test ();

Member Methods:

Test T = new Test (34);

T.setage (35);

Invoking the normal method with an instantiated object

Normal member variables:

Test T = new Test (34);

Because the variable age adds private, it cannot be called: T.age = 35; This is wrong and can only be modified by means of the method.

Global method:

Test.getage ();

Call it directly, of course, if not instantiated, Test.getage () here is 0

Three methods in a Class 1 global Method 2 member Method 3 construction Method (GO)

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.