This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about Enum . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about Enum ." Watch "Doubt about Enum ." New topic

Doubt about Enum .

Manishk Rai

Joined: Jul 06, 2007
Posts: 21
The following piece of code

prints the output

It is a account type
It is a account type

I think the correct o/p is
It is a account type
Because it calls the constructor ( private) implicitly .
But why one exta "It is a account type" .
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
When you first use the enum type, every single enum constant is initialized, that is, the constructor for this constant is called.

So when you try to print an AccountType constant in your main method, the constructor is called once for every initialization constant.

But after that it will never be called again, no matter how much you use the constants. So if we change the main method like this:

, the output is going to be:
sree visu
Ranch Hand

Joined: Jul 05, 2007
Posts: 133
a very nice answer .hats off...

Regards<br />Sree Visu<br />SCJP 5.0, SCWCD 5.0, SCJD(Preparing B&S 2.2.2)
Manishk Rai

Joined: Jul 06, 2007
Posts: 21
Thanks Serge
I agree. Here's the link:
subject: Doubt about Enum .
Similar Threads
Constructor issues with creating a new checking account object.
enum constructor help
Enum's doubt from Java Beat
ModelDriven/Hibernate validation issue