Enum is expanded by the compiler into a class statement?

Asked

Viewed 64 times

4

Enum is expanded by the compiler in a class declaration in Java?

  • 1

    What do you mean by expanded?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

1 answer

5

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

  • It has optimizations for some scenarios such as the use of bits,
  • it is allowed to use in some situations where the class is not, for example the switch,
  • cannot instantiate or extend type,
  • has serialization and toString() automatic,
  • I guess some other things.

So you could say yes, at least in the end it’s more or less the same thing. I don’t know if the specification says anything about how it should be done, but it’s a possible way, because I don’t know any specific instructions in bytecode of the JVM dealing with enumerations, then it has to be simulated with some other existing form and the class seems appropriate.

There’s a question in the OS whose answers confirm this.

  • What do you mean expanded in this case? I understand the explanation but still do not understand what this question wants to know.

  • What is translated after compiling

Browser other questions tagged

You are not signed in. Login or sign up in order to post.