下面这段解释不知你是否满意呢?
inner class的问题,inner class是否能够有static的变量?
一般的说法是,static inner class能够有static的变量,而non-static inner
class则不能有static的变量。但是,如果这样:
class outer {
        class inner {
                static final int i = 999;
        }
}
编译通过。而你不能这么写:
                static final int i;
就是说,一定要在声明它的时候赋予初值, 因为i是final的,它的值不能再被改变。
关于inner class, 还有一个代码也很能说明一些问题。
public class TestClass
{
    public TestClass(int i) { }    public void m1()
    {
        TestClass al = new TestClass(10) 
                       { 
                            public void actionPerformed(ActionEvent e)
                            {
                            }
                      };
    }
}
这说明了一下四个问题:
1. 类中的方法也可以有inner classes.(但是他们不能有static inner classes).
2. Inner class 能够继承包含它的外部class.
3. 匿名 inner class 可以继承一个 class 或者 implements 一个 interface。
4. 匿名 inner class 可以有初始化参数。(如果这个class继承的基类有相应的 constructor 的话。)

解决方案 »

  1.   

    呵呵,虽然你没解决我的疑惑,但是还要谢谢你的回复,分不会少.
    我的问题的关键就是这句:为什么non-static inner class不能包含static字段和方法(编译时常量除外).
      

  2.   

    其实你可以这样理解,inner class顾名思义就在一个类里面的类,所以,可以把内部类看成是类的一个成员方法,而成员方法是不能定义静态变量的。这样的解释可能是比较可以的了。
      

  3.   

    果然是个经典的问题,不过我觉得这应该问一问java的最初设计者。
      

  4.   

    搂主,你写个这样出错的简单代码来看看。
    别不是你在inner class 里面的static 方法里面包含了non-static 方法或者变量吧。
      

  5.   

    Inner classes ,unless static ,have an implicit reference to the enclosing instance.The enclosing instance must be provided to the new call that constructs the inner class.In many cases,innerclass are constructed inside instance methods of the enclosing class.in which case this.new is implied by new
      

  6.   

    对了,是我弄错了,和和,提示:仅仅定级class才能拥有static method.
      

  7.   

    qxjavajava(射手座) 说的有道理。
    非静态类中,可以有静态方法,但是也可以有非静态方法,如果两者都有的话,
    在调用非静态方法的时候,就会发生new的冲突,所以java为了避免这种混淆,所以就不允许在非静态方法中使用静态的方法和变量
      

  8.   

    根据qxjavajava(射手座)提供的信息,我是不是可以这样理解:因为non-static inner class必须经过实例化才可以使用,而如果其内部有静态成员,就要求其在未实例化的情况也可以使用(来调用静态成员),这样就造成了的矛盾。不知道我这样的理解对不对?
      

  9.   

    我觉得:想要调用非静态内部类,就要先实例化,这是就会和其中的static相矛盾。
      

  10.   

    经过水木清华一位站友的帮助,终于找到了java文档中一句关键的解释:This shows why an inner class cannot declare a static member, because the entire body of the inner class is in the scope of one or more enclosing instances. 而static是"top-level" entities. 这似乎应证了我上边的理解。谢谢大家的帮助。
      

  11.   

    其实,理论上,让non-static inner class支持静态变量也没什么问题。
    语义上说的通的。
    只不过是个name space, 和一些access control方面的东西罢了。
    为什么这样说呢?
    我们都知道,一个inner class, 只不过是个隐士拥有一个对outer class得引用而已。
    所以,
    class A{
       public void f();
       class I{
         public void g(){f();}
       }
    }
    里的I, 可以模拟为:
    class A{
       public void f();
    }
    class I{
       private final A outer;
       public void g(){outer.f();}
       I(A a){this.outer = a;}
       public static int i;
    }
    这里,除了inner class能自动拥有对outer class私有成员的访问权,以及名字空间上的区别,语义上真是太一样了。
    而这个新的I类,就是可以拥有静态成员的。之所以Java不支持这样做,我想原因是
    1。 语法上,即使有non-static inner class的静态成员,也没什么用。
    为什么呢?
    对上面例子中的inner class I类, 我们语法上怎么使用呢?A.I是肯定不行的。因为I是non-static的。
    所以,就一定要一个A的instance. 如new A().new I()之类。那么此时,你已经不需要静态成员了。因为无论如何,你还是被迫要生成实例。
    这里, Java对non-static inner class的语法,决定了它很难清楚简洁地表达一个静态成员的概念。除非Java针对这个需求,修改或增加它的语法规则。2。 对non-static inner class拥有静态成员的需求并不理直气壮。你为什么一定要这么个东西呢?你有那么多还不错的work-around嘛。而显然,Java并不想为了一个似乎没什么用的feature去增加语言的复杂度。