1. Access rules for internal classes:
(1) An inner class can access members of an external class, including private, so that members of the outer 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 cannot access the inner class directly, and to instantiate an external class object to access the contents of the inner classes
2. 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 another class outside the
You can create an inner class object directly
Format: outer class name. Internal class Name Variable name = External Class object. Inner Class object
For example: Outer.Inner obj = new Outer (). New Inner ();
(2). When the inner class is on a member position, it can be decorated with the member modifier.
For example: Private, encapsulating inner classes in the outer class
Static: The inner class has static properties
When the inner class is modified by static, only static members in the outer class can be accessed directly, and access restrictions occur.
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 be static
classouter{Private intx = 3; classinner{intx = 4; voidfunction () {intX =5; System.out.println ("Inner:" +outer. This. x);//3System.out.println ("Inner:" + This. x);//4System.out.println ("inner:" +x);//5} }}
Anonymous inner class:
1. Define the premise that the inner class must inherit a class or implement an interface
Anonymous inner class format: New parent class or interface {Defines the contents of a subclass}
In fact, the anonymous inner class is an anonymous subclass object, and the object is a bit fatter. Can be understood as an object with content
New Absdemo () {//absdemo is an abstract parent class
void Show () {System.out.println ("");} Overriding methods of the parent class
}. Show ();//The colored part is an object of an anonymous inner class
Anonymous inner class should not be more than 3, otherwise the readability is poor
Java Internal classes