Why the static methods values() and valueOf() in Enum are added by the compiler and not explicitly declared in the Enum class?

有些话、适合烂在心里 提交于 2020-01-24 09:39:30

问题


In reading through the Javadoc of Enum I can see a signature of valueOf() that is not what I usually use. Also the javadoc for values() method is also missing. I read that these methods are actually generated by the compiler automatically. So my question is why are these methods not declared as static methods in the Enum class itself? What is the need for the methods to be included like the way they are by the compiler? Why this extra layer of abstraction?


回答1:


These methods have to be declared not in Enum, but in the Enum subclass, the enum type you're writing. They can't be added any other way, not least since static methods don't inherit the way instance methods do.



来源:https://stackoverflow.com/questions/15452703/why-the-static-methods-values-and-valueof-in-enum-are-added-by-the-compiler

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