Java internal class definition principles and java definition principles

Source: Internet
Author: User

Java internal class definition principles and java definition principles

/*
Internal class access rules:
1. The internal class can directly access members in the external class, including private.
The reason for direct access to Members in the external class is that the internal class holds an external class reference in the format of external class name. this
2. To access internal classes, an internal class object must be created.

Access format:
1. When the internal class defines the member location of the external class and is not private, it can be in other external classes.
Internal class objects can be directly created.
Format
External class name. Internal class name variable name = External Class Object. Internal class object;
Outer. Inner in = new Outer (). new Inner ();

2. When the internal class is in the member position, it can be modified by the member modifier.
For example, private: encapsulate internal classes in external classes.
Static: Internal classes have the static feature.
After the internal class is modified as static, you can only directly access static members in the external class. Access restrictions have emerged.

In other external classes, how do I directly access non-static members of static internal classes?
New Outer. Inner (). function ();

In other external classes, how do I directly access static members of static internal classes?
Uter. Inner. function ();

Note: When a static member is defined in an internal class, the internal class must be static.
When the static method in the external class accesses the internal class, the internal class must also be static.




When describing a thing, there is something inside it. It is described by an internal class.
Because internal transactions use the content of external transactions.

Class Body
{
Private class XinZang
{

}

Public void show ()
{
New XinZang ().
}

}





*/
Class Outer
{
Private static int x = 3;


Static class Inner // static internal class
{
Static void function ()
{
System. out. println ("innner:" + x );
}
}

Static class Inner2
{
Void show ()
{
System. out. println ("inner2 show ");
}
}

Public static void method ()
{
// Inner. function ();
New Inner2 (). show ();
}

}


Class InnerClassDemo2
{
Public static void main (String [] args)
{
Outer. method ();
// Outer. Inner. function ();
// New Outer. Inner (). function ();
// Directly access members in the internal class.
// Outer. Inner in = new Outer (). new Inner ();
// In. function ();
}
}

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.