Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's enum... Where are they created?

Since enum in C# are on the stack, I was wondering where enum, in Java, where created. On the stack? On the heap? In some mysterious other place?

Enumeration in C# are more primitive than those in Java, this might explain why they are created on the stack...

Where are they? I can't find them!

Thanks

like image 374
ALOToverflow Avatar asked Sep 07 '10 16:09

ALOToverflow


People also ask

Where does enum start from?

The default value of Enum constants starts from 0 and increments. It has fixed set of constants and can be traversed easily. However you can still change the start index and customize it with the value of your choice. In the following example, I have set the customized value to be 20 instead of the default 0.

Where do you declare enums?

The best way to define the enum is to declare it in header file. So, that you can use it anywhere you want by including that header file during compilation.

Where enum can be defined in Java?

In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } You should use enum types any time you need to represent a fixed set of constants.

Where is enum stored in memory?

As enum types are handled as integral types, they are stored in the stack and registers as their respective data types: a standard enum is usually implemented as an int32; this means that the compiler will handle your enum as a synonym of int32 (for the sake of simplicity, I left out several details).


1 Answers

Enums in Java are also objects: for example, enums can have instance variables/methods/constructors and implement interfaces. All this makes me think they're handled just like other objects by jvm.

like image 158
Nikita Rybak Avatar answered Oct 09 '22 16:10

Nikita Rybak