• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumerator question

 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain why the below code outputs

CONSTANT_1
CONSTANT_2



and not just CONSTANT_1 ?
 
Stephan van Hulst
Bartender
Pie
Posts: 5596
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sumukh,

values() is a static method of your Enumerator class, that will return all declared enum constants. The fact that you call it on CONSTANT_1 is misleading, since it's a static method.
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan van Hulst,

I understand what you say about values() method.

But is it not that if I am setting the Enumerator through setEnumerator() then the class should have a Enumerator with just CONSTANT_1?

Please correct me if I am wrong.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values() method, that you are calling in line 42 on an instance of Enumerator, is a static method in Enumerator. Do you understand what "static" means? It means that it's a method that works on the whole class, rather than on a specific instance of the class.

You're confused exactly because of the reason that Stephan explains: it's misleading to call static methods on instances, because it looks like it should be doing something with the specific instance, while it is not. Calling values() on a specific instance will not make it work differently from calling it on the class itself.

Rewrite line 42 like this:


(I think it's a mistake that Java even allows you to call static methods on instances, because it's too confusing and bad style and isn't useful for anything).
 
Stephan van Hulst
Bartender
Pie
Posts: 5596
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:(I think it's a mistake that Java even allows you to call static methods on instances, because it's too confusing and bad style and isn't useful for anything).


Quoted for truth.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there were several mistakes like that. Permitting [] after the identifier rather than the type (eg int numbers[]; rather than int[] numbers;) is another, in my opinion.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: That's a feature that Java inherited from C++. When Java was new, it was the intention of the language designers that it looked familiar to C++ programmers. Now we're stuck with this feature forever...
 
Stephan van Hulst
Bartender
Pie
Posts: 5596
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[hijack]Oh man, what I really hate about C is when declaring a pointer type variable the asterisk goes with the variable and not with the type.[/hijack]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, Jesper. They didn't distinguish Java™ from C++ enough in the earlier stages.
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has cleared my doubt.

I had perfectly misunderstood the values() method.

Thanks a lot all of you.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic