• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • 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!
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7973
143
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!