This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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 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

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
    • 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
    • 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
    • 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
    • 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
    • Quote
    • Report post to moderator
    thanks Roel
     
    Raju Champaklal
    Ranch Hand
    Posts: 521
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Merry Christmas
     
    Sheriff
    Posts: 9671
    42
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rajiv please check your private messages...
     
    I am going to test your electrical conductivity with this tiny ad:
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!