• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Enum's doubt from Java Beat

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me whats the output for the following code?

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you not compiled and run the program?
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Barry/Sergio. I am pretty clear now.
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
brevity is the soul of wit - shakepeare. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!