/*
Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.
Access format:
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class.
You can create an inner class object directly.
Format
The outer class name. Internal class Name Variable name = External Class object. Inner class object;
Outer.Inner in = new Outer (). New Inner ();
2, when the inner class is in the member position, it can be decorated by the member modifier.
For example, private: Encapsulates inner classes in the outer class.
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly. Access restrictions have occurred.
In other external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();
In other external classes, how do you directly access static members of static internal classes?
Uter. Inner.function ();
Note: When a static member is defined in an inner class, the inner class must be static.
When a static method in an external class accesses an inner class, the inner class must also be static.
When describing things, there is something inside the thing, which is described by an inner class.
Because internal transactions are using the contents of external things.
Class Body
{
Private Class Xinzang
{
}
public void Show ()
{
New Xinzang ().
}
}
*/
Class Outer
{
private static int x = 3;
Static class inner//statically internal classes
{
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 accesses members in the inner class.
Outer.Inner in = new Outer (). New Inner ();
In.function ();
}
}
Definition principles for Java internal classes