本帖最后由 huyang936 于 2015-1-31 07:36 编辑
- class InnerClass
- {
- public static void main(String[] args)
- {
- Outer.Inner oi = new Outer().new Inner();//报错,创建静态内部类必须通过调用外部类名创建即:Outer.Inner oi = new Outer.Inner();
- //为什么内部类没加static前还可以通过外部类对象创建,加了static后就只能通过类名创建呢?
- }
- }
- class Outer
- {
- static int num = 100;
- static class Inner
- {
- public static void show()
- {
- System.out.println("Inner show..."+num);
- }
- }
- public void method()
- {
- new Inner().show();
- }
- }
复制代码 为什么内部类没加static前还可以通过外部类对象创建,加了static后就只能通过类名创建呢?
静态可以通过类名.静态成员调用,但也可以通过创建对象进行调用啊!为什么内部类不能呢?
|