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
錯誤:字段名稱不能被聲明爲static
解決這個?
您是否嘗試將'Constants'設爲靜態類型? –
不在常量中聲明字符串或使最終類常量也是靜態的(並且可能不會在主方法中聲明它)。 – Matthias