File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange but legal enum code

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Can anyone please explain step-by-step how this strange code works.

I've just found it on the web but I found it very strange.

I've never seen neither main() method within Enum nor abstract methods within Enum. Believe ! It's still valid.



Should I expect to find such kind of question on the real exam ?
 
J Sato
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope there's nothing like that on the test. I don't know how something like that would be useful in the real world. Just curious, where'd you find it?
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sato for your prompt reply.

Unfortunately I didn't take note the web site. I was in a cyber cafe and found that code and post on JavaRanch.
 
Vishal Chawla
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

hope this helps!!!

 
Vishal Chawla
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls feel free to add more and correct me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic