OUTPUT : Instance Block Constructor Instance Block Constructor Instance Block Constructor Static block Begin test MyValue from showValue in A : 10 MyValue from showValue in B : 20 MyValue from showValue in C : 30 Message from within static method Message from within static method Message from within static method 20
Just to confuse everyone more(give more knowledge) I modified some things in the existing code and added the output. Now the explanation.
Notice that for enums, you can have instance blocks, static blocks constructors and methods including static and sbstract.
Now inside the enumtest, enum constants have to be defined first. If you try to put any static blocks or instance blocks, you will get compile time errors.
Things are a little opposite here as compared to classes. Before main runs, the enum constants will be initialized.
Now in normal classes, the static block run first, then constructor and then instance blocks. For enums, the instance block runs first, then the constructor. These will run for each and every constant. Then the static block runs just once. The remaining code and outputs are self explanatory.
One more important thing and this i just learnt myself. I changed the modifier of int value to private from original protected. Once i do this, i will not be able to access this from the showValue method in each instance. So i have to use either a protected or public getValue() method that i added as well.