aspose file tools*
The moose likes Java in General and the fly likes question regarding enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "question regarding enum" Watch "question regarding enum" New topic
Author

question regarding enum

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
I have a code like so:


how to modify code above into enum Generator? thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It's not clear what you're trying to do or what problems you're having.
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
I'm trying to eliminate staic members.
so I can do something like this:
Generator.COUNTRY.get() will return country instance, while
Generator.STATES.get() will return states instance.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Make class Generator an enum: public enum Generator.

Declare two enum constants: COUNTRY and STATES.

Implement a body { ... } for each of the constants, in which you implement a get() method that returns whatever you want it to return.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239


I tried


got get() is undefined for type Generator.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

You can add an abstract method get() to the enum (Generator), but the problem with that is that the get() of your two constants return different types (Country and States). You could make the abstract method return a common superclass (Object if that's the closest common superclass of Country and States), but that would mean you need to cast when you call the method on each of the constants.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Hendra Kurniawan wrote:I tried
...
got get() is undefined for type Generator.

Jeff's right.

STOP CODING, back up, and explain to us what you're trying to do; not what you've done (which plainly isn't working).

So far, all I can tell you is that you are not using enum properly; and from the names you've provided, it sounds to me as though an Enum isn't even appropriate.

An enum might well be applicable to a list of Countries, but certainly not to States (assuming that this is a subdivision of a Country). States are actually quite difficult to model, since each country deals with them differently; but you should probably start by reading this.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
this class only has one purpose, instantiating other class, but returning the interface instead of the actual instance. but this is still trial and error, so to make things simple, I'm trying to return the actual instances for now. in short, I'm trying to instantiate both country and states. the class works just fine, but since I want to avoid the statics, enum seems prudent. however I'm having difficulty with instantiating country and states as you can see here. basically, I just want to find a solution to statics. thanks
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Both Jeff and Winston are right. You don't seem to explain exactly what you want to do.

From what I see or believe, enum may not even be the right approach. It looks like you are trying to have some kind of Factory class instantiating the Country and State.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
ah yes, that's the name of this approach, factory. so is it possible to use this approach without the statics? thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hendra Kurniawan wrote:this class only has one purpose, instantiating other class, but returning the interface instead of the actual instance. but this is still trial and error, so to make things simple, I'm trying to return the actual instances for now. in short, I'm trying to instantiate both country and states. the class works just fine, but since I want to avoid the statics, enum seems prudent. however I'm having difficulty with instantiating country and states as you can see here. basically, I just want to find a solution to statics. thanks


There are times when static methods are appropriate. "Bootstrapping" an abstract factory is generally one of those cases.

This definitely does NOT look like a case for an enum. An enum is a class just like any other, with a few special features and restrictions.

With normal classes, you instantiate as many as you want, whenever you want, and any given instance can have any possible valid state, and there's nothing to identify one instance as distinct from another except whatever references you hold onto and whatever internal state you give them and enforce uniqueness upon.

The purpose of an enum, on the other hand, is to establish at compile time a fixed, well-known set of instances. Like a class or an interface, an enum defines a type, but it's a type where the definition also includes, "...and here are the only instances of that type that can ever exist."

Yes, you could cram your square requirements peg into that round purpose hole, but it would be ugly and pointless. You'd have to have your methods return Object, as already stated, or else have Country and States (should probably be "State", by the way, singular) implement some common interface and have the methods declared to return that type (and declare a matching abstract method on the enum). But that only makes sense if you're going to use the Country instance and the State instance via the interface, without casting.

It's good to say, "Is there a better way to do this, without using static methods?" but in your case, the answer seems to be "No."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hendra Kurniawan wrote:ah yes, that's the name of this approach, factory. so is it possible to use this approach without the statics? thanks


An approach that's sometimes used, but may be overkill for you, it to define a static method that gets a factory (usually reads some properties files, etc., to determine which factory to instantiate). That method returns an instance of a factory class, and that factory instance then instantiates the type you're looking for.

Again, it is common and acceptable practice to use static methods for factories.



Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
(sigh) so there's no way doing this without statics? I was so happy when googled about enum for this case. but as you said, the enum only returned the interface, while classes I want to instantiate don;t neccesarilly implement the same interface. thanks anyway.

FYI: I know it's supposed to be State (without additional s), but I don;t want any ambiguation with Java's native State class
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hendra Kurniawan wrote:(sigh) so there's no way doing this without statics?


You can, but it will involve ugly casting and/or forcing your Country and States into an unnatural common type hierarchy.

Static methods do have their place.

FYI: I know it's supposed to be State (without additional s), but I don;t want any ambiguation with Java's native State class


Fair enough. I didn't know there even was such a class. I'd probably go a different route though, since it can look like it's related to Java's State class. (Array/Arrays, Collection/Collections...) Something like MyDomainState (where MyDomain is replaced by whatever is meaningful for your context).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Hendra Kurniawan wrote:(sigh) so there's no way doing this without statics?

Well, like I said above, I think an enum of countries (Country?) is quite reasonable, since they don't change too often - and you probably only need a subset of them anyway; but "states" are another thing.

Did you read the article I suggested? Because there are probably data files out there in Internet-land based on ISO 3166-2 that can provide you with the basis for a "States" class - and personally, I'd have a States class that contains ALL states, and a State class that defines an individual state or province (like Jeff, I was unaware of an existing class, so I suspect it's rare enough not to worry about - one is in javax.swing.plaf.nimbus, and the other is in org.omg, neither of which are used too often).

Winston
Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
okay, thanks guys. I guess in the mean time, I have to go back to statics.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
One of my rules of thumb is that whenever anybody says it works if I make it static, there is something seriously wrong with his design.
Things can be static because they need to be static, but if you can’t explain why they should be static, they shouldn’t.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question regarding enum