aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about Enum ." Watch "Doubt about Enum ." New topic
Author

Doubt about Enum .

Manishk Rai
Greenhorn

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

prints the output

It is a account type
It is a account type
SAVING

I think the correct o/p is
It is a account type
SAVING
Because it calls the constructor ( private) implicitly .
But why one exta "It is a account type" .
Thanks
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
Greenhorn

Joined: Jul 06, 2007
Posts: 21
Thanks Serge
 
 
subject: Doubt about Enum .