【发布时间】:2013-12-12 21:44:18
【问题描述】:
我知道这取决于编写匿名类的上下文(静态或非静态方法)。 但是看看这部分代码:
public class A {
int fieldOfA;
private static class B {
int fieldOfB;
}
public static void main(String[] args) {
B obj = new B() { //this anonymous class is static becuase is in the main method.
private static void testMethod() { //so why here i have an error and i can put just a non-static method
//if my class is static ?
//a class static can have static method, but this class no, why?
}
};
}
}
确定匿名类是静态的吗?
【问题讨论】:
-
该类不是静态的,它是主函数的本地类。你在哪里找到这个定义的?
-
我们已经在 4 天前进行了这场战斗。
-
静态类是用'static'关键字显式标记的。
-
并且标有
static关键字的类是静态的。
标签: java static-class