【发布时间】:2013-08-30 06:07:59
【问题描述】:
public class Application {
public static void main(String[] args) {
final class Constants {
public static String name = "globe";
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Constants.name);
}
});
thread.start();
}
}
编译错误:The field name cannot be declared static in a non-static inner type, unless initialized with a constant expression
解决办法?
【问题讨论】:
-
您是否尝试将
Constants设为静态类型? -
不在常量中声明字符串或使最终类常量也是静态的(并且可能不在主方法中声明它)。
标签: java multithreading static inner-classes