Enum is expanded by the compiler into a class declaration?

4

Does Enum expand by compiler into a class declaration in Java?

    
asked by anonymous 08.12.2016 / 14:23

1 answer

5

It can be understood that a Enum is a class with special characteristics. The compiler will always treat this type in a specific way.

  • It has optimizations for some scenarios like the use of bits,
  • It is allowed to use in some situations where the class is not, for example switch ,
  • can not instantiate or extend type,
  • have automatic serialization and toString() , I think a few more things.

So you can say that yes, at least in the end it gives more or less the same thing. I do not know if the specification says something about how it should be done, but it's a possible way, even though I do not know any specific instructions in the bytecode of the JVM that deals with enumerations, so it has to be simulated in some other way and the class seems appropriate.

There is a question in the OS whose answers confirm this .

    
08.12.2016 / 14:36