Missing code implementation in the compiler [message #1868088] |
Thu, 11 July 2024 10:33  |
Eclipse User |
|
|
|
Hello. This error is not critical, but this difference in behavior confused me. The problem is in the code that creates an anonymous class, inside of which there is a static method that accesses a local variable. javac does not compile such code at all, while eclipse compiles the code successfully if assignment is constant.
I.e. if testLocal = foo() javac gives 'error: non-static variable testLocal cannot be referenced from a static context' and eclipse gives 'missing code implementation in the compiler'.
If testLocal = 1 javac gives the same error but eclipse compiles code successfully and code can be executed.
public class Main {
private static int foo() {
return 1;
}
public static void main(String[] args) {
final int testLocal = 1;
Object obj = new Object() {
public static void foo() {
System.out.println(testLocal);
}
};
try {
obj.getClass().getMethod("foo").invoke(null);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03319 seconds