• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enum

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it true that the constructors in an enum are run only when the enum is first loaded and not when a new instacne is created of the constants



why such a strange result?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ankit: removed long quotes]

You can NEVER invoke an enum constructor directly. The enum constructor
is invoked automatically, with the arguments you define after the constant
value.
you are invoking constructor by creating instance Coffee c=Coffee.BIG;
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so you mean even this(1,"efdew") wont work...because we cant call a contrcutor like this only automatically gets called
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, thats correct.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raju,

Even this program
will result in a similar output:

You can NOT call an enum constructor directly. So in my main method I can't addThis will result in a compiler error.

My no-arg constructor invokes without any problem a call to the 1-arg constructor with this("A"), so that's no problem. Which constructor will be invoked is defined by your set of constants you are declaring. From my example:
  • SMALL --> 1-arg constructor
  • BIG and LARGE --> no-arg constructor

  • And I think the reason why is clearly obvious.

    Kind regards,
    Roel
     
    Raju Champaklal
    Ranch Hand
    Posts: 521
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    am i too early or too late to be learning new stuff ...exam in 6 days
     
    Neha Daga
    Ranch Hand
    Posts: 504
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I guess its never late...but dont get confused.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Raju Champaklal wrote:am i too early or too late to be learning new stuff ...exam in 6 days


    You are never too late to learn something new. Even if you learn something new after 2 weeks, it is only too late for the exam

    Good luck with your exam!
    Merry Christmas and a Happy New Year!

    Kind regards,
    Roel
     
    Rajiv Chopra
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [Ankit: removed long quotes]

    Thanks Roel for this example.
     
    Neha Daga
    Ranch Hand
    Posts: 504
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks Roel
     
    Raju Champaklal
    Ranch Hand
    Posts: 521
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Merry Christmas
     
    Sheriff
    Posts: 9707
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Rajiv please check your private messages...
     
    He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic