Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Enum .

 
Manishk Rai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a very nice answer .hats off...
 
Manishk Rai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Serge
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic