Java - Difference between private and package-private enum constructor [duplicate]

那年仲夏 提交于 2019-12-31 02:42:12

问题


Recently I'm quite oftenly using Enumerations. So I wonder...

Is there any difference between a private Enum constructor and a enum constructor withour any visibility modifier (package-private)?


回答1:


According to java docs

The constructor for an enum type must be package-private or private access.

but Accroding to the JLS

If no access modifier is specified for the constructor of an enum type, the constructor is private.

So there no difference between the package-private and private .




回答2:


The constructor for enums is implicitly private, just like methods for interfaces and annotations are implicitly public abstract. The default is package local for class members.

BTW enum classes are implicitly final and nested enum classes are implicitly static.

Older constructs tend to allow you to add implicit modifiers, but newer constructs don't allow you to say. e.g. enums are final but you can't add final to an enum.



来源:https://stackoverflow.com/questions/18704909/java-difference-between-private-and-package-private-enum-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!