aspose file tools*
The moose likes Java in General and the fly likes enum extending 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 "enum extending enum?" Watch "enum extending enum?" New topic
Author

enum extending enum?

Samaresh Gupte
Greenhorn

Joined: Oct 03, 2009
Posts: 20
Hi,

I thought of using enums for declaring mostly String constants as suggested by Sonar report.

After delving further I realised lot of code is being repeated in every enum I declare. I have some enum declared in a class to overcome the namespace. For e.g.


Observe that class A has two enums viz. TridionConstants and CacheConstants
both these enums have certain code common.

I am wondering whether I can pull the commonalities into a SuperEnum and have those enums extend/implement SuperEnum.

Jesper: Fixed code tags


Regards,
Samaresh Gupte
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Enums implicitly extend java.lang.Enum, so you can't have an Enum that extends another Enum, because a class can only extend one other class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Samaresh Gupte wrote: . . . I thought of using enums for declaring mostly String constants as suggested by Sonar report. . . .
Why? If you need String constants, use String constants.But why do you want String constants in the first place? An enum which doesn’t use Strings might be a better solution.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18134
    
    8

As far as I can see, most of that code is dedicated to making the result of toString() come out different than the default for enums. What's the point of that?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
I've seen this pattern a few times - it can make sense if the strings represent something found outside Java code and outside your control, such as constants used in a database (e.g. possible values of a field), and the established values of these constants do not match Java naming conventions for enums. This also assumes the list of allowed values does not change often, and if it does change, you need to release a new version of the enum to handle the changes. Which can be a pain. But, it's nice to be able to use the enum within Java code, especially if there is other behavior you want to attach. If this matches your situation reasonably well, enums with alternate String names can make sense. And yes, there's some repeated code in there, because enums can't extend anything other than the Enum class, and Java doesn't have mix-ins, so you can't really inherit any other behavior. I don't think there are any good ways to reduce the amount of code here. It's not too bad though.

I would note that many of us regard it as Wrong™ to use a name like CONSTANT_VALUE for a field that is not a compile-time constant, and is not static, but is simply an immutable instance field. That's misleading to those of us used to standard naming conventions. Also I would prefer not to override toString(), which is changing the expected behavior for an enum, but instead add a new method, like getName() of getDbValue(). (The name of such a method can also be useful in telling people what you need this alternate String for.) But that's more of a personal preference, as the toString() contract of Enum is flexible.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Campbell Ritchie wrote:Why? If you need String constants, use String constants.


I think the key question here is whether these form distinct value sets. If you only want a way to refer to a String value in code to avoid magic string, then a String constant is all you need. If you're going to have variables that can only take a value out of one of these sets, then an enum is a good idea (I think).

For example, will there ever be variables of type CacheConstants? Or will we just be using it to refer to CacheConstants.MEM_CACHE and the like?
Samaresh Gupte
Greenhorn

Joined: Oct 03, 2009
Posts: 20
Thanks Matthew. That sounds good.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum extending enum?
 
Similar Threads
Enum Doubts
Constructor in enum
Messy enum problem
Instantiating an enum
Java enums, space, and display values