my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Enumerator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enumerator question" Watch "Enumerator question" New topic
Author

Enumerator question

Sumukh Deshpande
Ranch Hand

Joined: Feb 17, 2008
Posts: 87

Can you please explain why the below code outputs

CONSTANT_1
CONSTANT_2



and not just CONSTANT_1 ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Feb 17, 2008
Posts: 87

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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

Joined: Sep 20, 2010
Posts: 3647
    
  16

[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

Joined: Oct 13, 2005
Posts: 38865
    
  23
Agree, Jesper. They didn't distinguish Java™ from C++ enough in the earlier stages.
Sumukh Deshpande
Ranch Hand

Joined: Feb 17, 2008
Posts: 87

It has cleared my doubt.

I had perfectly misunderstood the values() method.

Thanks a lot all of you.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enumerator question