-
Bug
-
Resolution: Unresolved
-
P3
-
8, 9, 10, 11, 12
(See https://mail.openjdk.java.net/pipermail/compiler-dev/2019-February/012958.html )
Steps to reproduce: compile and run the following program:
// Test.java
class Test {
enum A{
X;
static { System.out.println("A is initialized!"); }
}
enum B{
Y;
static { System.out.println("B is initialized!"); }
}
static void testA(A a) { switch(a) {} }
static void testB(B b) { switch(b) {} }
public static void main(String[] args) {
testA(A.X);
}
}
Expected result:
A is initialized!
Actual result:
A is initialized!
B is initialized!
According to JLS 12.4.1 the B enum type should not be initialized, as its fields and methods are not referenced during the program's execution. It appears that the mere declaration of method `testB(B)` is enough to make javac think, wrongly, that B needs to be initialized.
Steps to reproduce: compile and run the following program:
// Test.java
class Test {
enum A{
X;
static { System.out.println("A is initialized!"); }
}
enum B{
Y;
static { System.out.println("B is initialized!"); }
}
static void testA(A a) { switch(a) {} }
static void testB(B b) { switch(b) {} }
public static void main(String[] args) {
testA(A.X);
}
}
Expected result:
A is initialized!
Actual result:
A is initialized!
B is initialized!
According to JLS 12.4.1 the B enum type should not be initialized, as its fields and methods are not referenced during the program's execution. It appears that the mere declaration of method `testB(B)` is enough to make javac think, wrongly, that B needs to be initialized.
- duplicates
-
JDK-8242785 Enum switch incorrectly initializes all enum switches in a class
- Closed