Default or initial value for a java enum array

As others have said, enums are reference types – they’re just compiler syntactic sugar for specific classes. The JVM has no knowledge of them. That means the default value for the type is null. This doesn’t just affect arrays, of course – it means the initial value of any field whose type is an enum is also null.

However, you don’t have to loop round yourself to fill the array, as there’s a library method to help:

Day[] days = new Day[3];
Arrays.fill(days, Day.MONDAY);

I don’t know that there’s any performance benefit to this, but it makes for simpler code.

Leave a Comment