• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

enum extending enum?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Master Rancher
Posts: 4321
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew. That sounds good.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic