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

Error "The enum ... already defines the method ... implicitly" when declaring values() and valueOf()

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is my Enum:


My Instructor requires me to Implement the values() and the valueOf() methods. I scoured the Internet and found what is in the code.
Yet I get the error:
"The enum WohnungOderGewerbe already defines the method values() implicitly"
The same goes for valuesOf().

What is happening? How can I implement error free?

Thanks!
 
Marshal
Posts: 27450
88
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
Assuming your instructor is competent and knows what he's doing, then the way to implement those methods is to do nothing. As the compiler says, the methods are already (implicitly) defined and you can't redefine them.

If your instructor doesn't like that then you have a problem with your instructor. However you should discuss the issue to make sure there isn't some kind of misunderstanding before I start saying unjustified bad things.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if I leave the methods empty, I still get that error. Should I leave it all empty? You mean leave the Methods completely out?
Becaude they are in his JavaDoc for that class....
 
Paul Clapham
Marshal
Posts: 27450
88
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

Dennis Von Valkenburgh wrote:You mean leave the Methods completely out?



Yes.

It appears that your instructor didn't specifically say "You must implement the values() and valueOf() methods", as your original post led me to believe. It appears that you've just been given some Javadoc and told to implement that, which is a lot different.
 
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Von Valkenburgh wrote:


Apart from the "already defined" error, that wouldn't have worked. Enum.clone() is final and always throws a CloneNotSupportedException (which is checked, so you would have gotten another compiler error there). Apart from that, if it wouldn't throw an exception, it would have returned an enum instance, not an array of enum instances (which you are casting to). And in the implementation of ApartmentOrCommercial.values(), you call ApartmentOrCommercial.values(), which would result in a StackOverflowError.

FYI, if you would need to implement values(), you would use ApartmentOrCommercial.class.getEnumConstants().
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Dennis Von Valkenburgh wrote:


Apart from the "already defined" error, that wouldn't have worked. Enum.clone() is final and always throws a CloneNotSupportedException (which is checked, so you would have gotten another compiler error there). Apart from that, if it wouldn't throw an exception, it would have returned an enum instance, not an array of enum instances (which you are casting to). And in the implementation of ApartmentOrCommercial.values(), you call ApartmentOrCommercial.values(), which would result in a StackOverflowError.

FYI, if you would need to implement values(), you would use ApartmentOrCommercial.class.getEnumConstants().



The original Method as defined in his Javadoc is:



Would that even work?

Because according to him the goal is to "Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:" and then follows above method.
 
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like your professor is a bit zerstreut
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It looks like your professor is a bit zerstreut


Lol why is he zerstreut? Or maybe just a little durcheinander?
 
Stephan van Hulst
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think he wrote that code in the doc without compiling it, and was thinking of two different things at the same time when he wrote it. It's not correct. It also doesn't make sense to define the values() method on an Enum, because it will automatically be implemented by the compiler, along with valueOf(String).
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic