Inner classes
Why inner class?
In describing things, if an internal thing also contain other things that may be contained, for example, in describing the car, the car also contains this engine in this thing, then the engine can be used to describe the inner class. That is the internal things that must be hosted in external things inside.
class Outer{
//外部类的成员变量
int num = 5;
//写在Outer成员位置上的内部类
class Inner{
//内部类的成员函数
void show(){
//在内部类中访问外部类的成员变量
System.out.println("Outer num = "+num);
}
}
}
Inner class access rules
- Inner classes have direct access to members of the outer class, but the class can not access external inner class directly, to access, you must create an internal class object to access
public class Test {
public static void main(String[] args) {
Outer out = new Outer();
out.method();
}
}
class Outer{
int num = 5;
class Inner{
//内部类的成员函数
void show(){
//在内部类中访问外部类的成员变量
System.out.println("Outer num = "+num);
}
}
public void method(){
//创建内部类对象,访问内部类的成员函数或者变量
Inner in = new Inner();
in.show();
}
}
External class can not directly access the internal class Outer Inner, to create an internal class object in the function method, and then call the method functions by an external class that implements external access to internal class category.
Non-static non-private inner class
If a non-static inner classes, and non-proprietary rights, you can access outside of the outer class other classes. That can complete access to the internal classes, visit the following format by creating an external class:
//通过创建外部类对象,接着创建内部类对象
Outer.Inner in = new Outer().new Inner();
in.show();
Static non-private inner class
When the internal class is modified static class members in external position, due to the static you can directly use the class name to call, create a class object inside ways:
//因为内部类是静态,所以不需要创建Outer的对象。直接创建内部类对象就哦了。
Outer.Inner in = new Outer.Inner();
in.show();
Static member access static inner classes
Since the inner class is static, you can directly use the class name to call external inner classes, and members of the inner class is static, then the same can be invoked by a static member class name inside the class.
//既然静态内部类已随外部类加载,而且静态成员随着类的加载而加载,就不需要对象,直接用类名调用即可
Outer.Inner.show();
Using a modified static internal class, which belongs to the class internal external class, not part of the object outside the class;
static inner class may include a static member may also include a non-static member. Under static members can not access non-static members, so static inner classes can not access the outer class instance members can only access static members outside of class.
Note : non-static inner classes can not define static member variables and static member functions. However, you can define a static constant member. Reason: Constant replacement directly into a corresponding digital file when generating bytecode.
When the private inner class is modified in an external class member position, in other places outside the class of accident is not accessible. It can only be accessed outside the class.
Can directly access the internal class members outside of class, then class holds because the internal reference (external class .this) outside of class. For static inner classes do not hold classes outside .this use the external class name