aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum's doubt from Java Beat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum Watch "Enum New topic
Author

Enum's doubt from Java Beat

Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Can any one tell me whats the output for the following code?



Regards,
Durga Prasad
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you not compiled and run the program?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Hi Barry,
Ofcourse i compiled and run the program....!

Ok let me put it straight forward.

The answer for this is given as
It is a account type
It is a account type
It is a account type
FIXED

I dont understand why the constructor is being called three times here.


Can any one suggest?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
We have cases where people do not try to compile code and run it, so it's always best to ask.

So it looks as if the values of the enum are actually instances of a class, and what you are seeing is the constructor being invoked for each of the three instances. If you take a look at Sun's tutorial on Enums you will see a more interesting example where you can actually pass arguments to the constructor, and add methods to the Enum values.

Here's the Enum tutorial
[ May 30, 2007: Message edited by: Barry Gaunt ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

It must be that each of the enumerated values is an instance of the type AccountType class.

There must also be a toString() method that is created for you automatically.

Kaydell
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

The compiler will translate the enum type in the first post to something like this:



Also note the absence of a toString method. This means that if you don't supply one, your enum class will inherit the one from Enum class.

I hope this helps you as much it helped me understand what enums are in the end (e.g. ordinary java classes).

(Edit:RED,GREEN,BLUE changed to SAVING,FIXED,CURRENT in static block)
[ May 30, 2007: Message edited by: Barry Gaunt ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
So it looks as if the values of the enum are actually instances of a class, and what you are seeing is the constructor being invoked for each of the three instances.


I agree with this. But my dought is, in the code snippet i tried to call only one of its instances. So, it should invoke the constructor only once.
Isn't it.??

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you look at the post by Sergio you will see that there is a static initializer block that creates all the instances at once.

By the way, I guess that Sergio forgot to change RED,BLUE,GREEN to the types of account used in the rest of the enum implementation example

[ May 30, 2007: Message edited by: Barry Gaunt ]

OK - I'll edit Sergio's post for him.
[ May 30, 2007: Message edited by: Barry Gaunt ]
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Thanks a lot Barry/Sergio. I am pretty clear now.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Barry Gaunt:
By the way, I guess that Sergio forgot to change RED,BLUE,GREEN to the types of account used in the rest of the enum implementation example


Sorry. I forgot to change those from the original example.

Originally posted by Barry Gaunt:
OK - I'll edit Sergio's post for him.


Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum's doubt from Java Beat