How long have enum constructors' default access modifier been "private"? Since the beginning or did it change in Java 8? If it did, then what was the default access modifier of a constructor declaration with no access modifier? (I'm guessing it is the default(package accessible) like other java classes' constructors.)
I found some reference documents related to this, but couldn't find the exact answer. Here's what I've found,
From when enum constructor's default access modifier be "private"?
Always. You can't make new enums, that's sort of the point of them. You can't have a non-private constructor of enums (with javac from openjdk16):
> cat Test.java
public enum Test {
FOO, BAR;
public Test() {}
}
> javac Test.java
Test.java:3: error: modifier public not allowed here
Let's try with java6 (Yes, I have javac6 lying around, and can run it on new javas).
> java -jar javac6.jar -bootclasspath openjdk6-rt.jar Test.java
Test.java:3: error: modifier public not allowed here
So, that error hasn't changed in 2 decades.
Let's check the access level in javac6!
> cat Test.java
public enum Test {
FOO, BAR;
Test() {}
}
> java -jar javac6.jar -bootclasspath openjdk6-rt.jar Test.java
> javap -c -private Test
.... lots of decompiled bytecode
private Test();
Code:
0: aload_0
1: aload_1
2: iload_2
3: invokespecial #18 // Method java/lang/Enum."<init>":(Ljava/lang/String;I)V
6: return
... more decompiled bytecode
Thus, private. Always been, Always will be. It's like asking: "Did circles ever have corners?".
The answer is: No. Never ever. If ever a circle had a corner, it ceases to be one. An enum with a public constructor isn't an enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With