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

Java OCP study Guide: enum

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

I try to find out why I can't combine two enums with a constructor value and an abstract method;

This is compiling and running:


But now I want to combine the enum value's SPRING and FALL, because they have for example the same values:

This doesn't compile
I've tried a lot of different things but everytime compiling errors. I couldn't find out what the right way.
They gave an another example but than without constructor values like "Low", "Medium", that is compiling
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to go through the JLS (=Java® Language Specification)←click this link to check what the grammar is. I suspect that declaring a constructor for the class and calling it via WINTER, SUMMER, and FALL means that the class underlying the enum only has a one‑argument constructor, and you are not calling that constructor via SPRING. SPRING would then expect to use a no‑arguments constructor. At least I think that is the problem.
The precedence of () is higher than that of comma, so the () after FALL are not applicable to SPRING.
 
Nico van de Kamp
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie, I understand this, in how I the code have that is a problem.

Next example from Java 17 Study Guide, is working but without constructor value with SPRING and FALL put together:

But is this also possible with a constructor value? I've been through that site of Oracle but there they don't have situation with a constructor and combine two or more enum values with implementing one or more methods.


My expectation is that two enum objects will be created with the same constructor value "medium" and both implemented with same method...

But maybe is this not possible...
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the compiler had problems with



then I might have a try with



but that would depend on what the compiler messages said.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic