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 ();
}
}