I have seen a java code where I see a non-modified constructor in an enum. Can anyone tell me why it is like that?
public enum myEnum{
myEnum()
{
System.out.println("Hello World");
}
}
Assuming you mean why there is no access modifier for the constructor, that's because
In an
enumdeclaration, a constructor declaration with no access modifiers isprivate.
and
It is a compile-time error if a constructor declaration in an enum declaration is
publicorprotected(§6.6).
It's redundant to specify private, so some people don't.
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