Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Constructor in enum

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output-
Color is called RRR
Color is called YYY
Color is called GGG

but i expect that only Green constructor should be invoked and output would be "Color is called GGG"

Moreover if i create an overloaded constructor with two parameters and if only that is invoked ,we get rest of the constructors called too.

Does it happen in case of enum unkile to class contructors?
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enums are classes. The compiler just treats them a little special. Their constructors are the same. The difference is in how the instances can be created. Class instances are created using the new operator by any other code that has access to the class (and a couple other ways that are not relevant to this conversation). The compiler prevents you from using the new operator on enums (it adds protection even greater than defining a constructor as private).

The compiler (under the covers) defines and initialises one static final reference for each enum element that you define. For example, you defined an enum called Color and defined the Color elements RED, WHITE, and BLUE (well, I changed the colors for the good ol' USA). Then only three static final references are defined and initialized by the compiler. So enum instances are created the same as any other object that must be assigned to a static reference - before any code uses the class (or enum).

As an illustration of enums being classes, I modified your code as follows. As you mentioned, there can be multiple constructors. One constructor can even call the other one. An enum can override methods and can even have a main() method. To show when enum instances are created I modified the printlns to illustrate.


BTW, the static final instances are also the reason why you can use the == operator to compare two enum references.

Also BTW, I started to answer your question and then got carried away. I hope I did not add confusion.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tom.

output of your program is -

First reference to Color is next
this.color = Red
this.color = White
this.color = Blue
this.favorite = true
COLOR.RED is Red

So do i consider that all the constructors of enum are invoked with a single call of an enum.

As at line 17 we call only RED but WHITE is called automatically.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bobal wrote:

Output-
Color is called RRR
Color is called YYY
Color is called GGG

but i expect that only Green constructor should be invoked and output would be "Color is called GGG"

Moreover if i create an overloaded constructor with two parameters and if only that is invoked ,we get rest of the constructors called too.

Does it happen in case of enum unkile to class contructors?



Hello Deepak I was precisely studying enums yesterday, you know enum constructors are automatically INVOKED.
Modify your code like this and the out-put will be GGG:

 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic