Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ENUM storage (memory, etc)

I was just asking myself a little question, and I'm not sure if I can find the right answer to this:

If I use an ENUM in Java, with an own constructor (and many, many, maaaaany parameters) - are those stored in Memory every time the program gets executed, or are they only 'loaded' in Memory, if they get used?

What I mean is, if I have an ENUM with 400 entries, and only use one of the entries - are all others still present in Memory?

some pseudocode:

public enum Type {
    ENTRY_A(val1, val2, val3, val4, new Object(val5, val6, val7, ...)),
    ENTRY_B(val1, val2, val3, val4, new Object(val5, val6, val7, ...)),
    ENTRY_C(val1, val2, val3, val4, new Object(val5, val6, val7, ...)),
    ...
}

If I only use ENTRY_A, and dont touch ENTRY_B, ENTRY_C, etc - how will Java handle that exactly?

Thanks for the Answer - and yes, this is mainly curiousity

like image 800
Katai Avatar asked Jan 15 '23 05:01

Katai


1 Answers

Yes everything will be loaded to memory even though you use only one ENUM constant.

Enum is special type of class and all fields are constants, so when you load the ENUM, then itself it loads everything to memory.

like image 70
kosa Avatar answered Jan 27 '23 13:01

kosa