Ostateczne deklaracje statyczne Java w klasach lokalnych metod
Kiedy deklarujesz lokalną klasę wewnętrzną wewnątrz metody, dlaczego legalne jest dołączanie końcowych statycznych ciągów znaków lub int, ale nie jest to legalne, aby uwzględnić inne obiekty?
Na przykład:
class Outer {
void aMethod() {
class Inner {
final static String name = "compiles";
final static int ctr = 10; // compiles
final static Integer intThree = Integer.valueOf(3); // does not compile!
final static obj objConst = new Object(); // does not compile!
}
Inner inner = new Inner();
}
}
Kiedy to kompiluję, otrzymuję następujące informacje:
InnerExample.java:6: inner classes cannot have static declarations
final static Integer outer = Integer.valueOf(3);
^
InnerExample.java:7: inner classes cannot have static declarations
final static Object objConst = new Object();
^
Dlaczego to rozróżnienie? Czy dlatego, że String jest niezmienny? Jeśli tak, to czy Integer.valueOf () również nie będzie ważny?