This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange but legal enum code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange but legal enum code" Watch "Strange but legal enum code" New topic
Author

Strange but legal enum code

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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 ?


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
J Sato
Ranch Hand

Joined: Mar 30, 2006
Posts: 40
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

Joined: Jan 19, 2006
Posts: 584
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

Joined: Apr 06, 2006
Posts: 28


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

Joined: Apr 06, 2006
Posts: 28
pls feel free to add more and correct me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange but legal enum code