public class Outer{
//just like static method, static member class has public/private/default access privilege levels
//access privilege level: public
public static class Inner1 {
public Inner1() {
//Static member inner class can access static method of outer class
staticMethod();
//Compile error: static member inner class can not access instance method of outer class
//instanceMethod();
}
}
//access privilege level: default
static class Inner2 {
}
//access privilege level: private
private static class Inner3 {
//define a nested inner class in another inner class
public static class Inner4 {
}
}
private static void staticMethod() {
//cannot define an inner class in a method
/*public static class Inner4() {
}*/
}
private void instanceMethod() {
//private static member class can be accessed only in its outer class definition scope
Inner3 inner3 = new Inner3();
//how to use nested inner class
Inner3.Inner4 inner4 = new Inner3.Inner4();
}
}
class Test {
Outer.Inner1 inner1 = new Outer.Inner1();
//Test and Outer are in the same package, so Inner2 can be accessed here
Outer.Inner2 inner2 = new Outer.Inner2();
//Compile error: Inner3 cannot be accessed here
//Outer.Inner3 inner3 = new Outer.Inner3();
}
--------------------next---------------------
阅读(328) | 评论(0) | 转发(0) |