Chapter 8 (4) Java internal class and Anonymous class, package and Java garbage collection

Source: Internet
Author: User

1). Internal class

An internal class is a class contained in a class, also known as a "nested class". Classes containing internal classes are also called "external classes ". an internal class can be considered as a member of an external class, so the internal class also becomes a "member class ".

When an internal class is referenced externally, the internal class name must be prefixed with the external class name to be used. When an internal class is created using the new operator, the object variable must be prefixed with the new class name.

Eg: access rules for internal and external classes.

Public class group

{

Private int age; // declare the private member variable of the external class

Public class student // declare internal class

{

String name; // declare the member variables of the internal class

Public student (string N, in a) // defines the internal class Constructor

{

Name = N; // Name of the member variable accessing the internal class

Age = A; // access the age variable of the external class

}

Public void output () internal class member Method

{

System. Out. println ("name:" + this. Name + "; age:" + age );

}

}

Public void output () // defines the member methods of the external class

{

Student Stu = new student ("Liu Yang", 24); // create an internal Class Object Stu

Stu. Output (); // call the member method of the internal class through Stu

}

Public static void main (string [] ARGs)

{

Group G = new group ();

G. Output ();

}

}

The program running result is as follows:

Name: Liu Yang; age: 24

 

 

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.