Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3965918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:31:47+00:00 2026-05-20T03:31:47+00:00

I’m trying to write some Java code for a set of enum classes. Each

  • 0

I’m trying to write some Java code for a set of enum classes.

Each of the enums encapsulate some conceptually distinct data, so it doesn’t make sense to combine them. The enums also map to values in a database, and so also share some common operations, both instance and static operations, related to loading data from the database.

I need to generalise the set of enum classes I have, such that I can pass any one of these enums into a different class which performs and caches of database lookups relating to each of the different enums.

Since the cache/lookup class will also depend on the public and static methods defined in each enum, how can I code my solution so that I can guarantee that any enum that can be passed into the class will have the required methods?

The normal approach would be to define an interface, but interfaces don’t allow static methods.

Alternatively, you might use an abstract class to define the interface and some of the common implementation, but I don’t believe that is possible with enums (I understand that enums must extend the Enum class and cannot be extended).

What are my options do I have that enable me to ensure all of my enums implement the methods I need?

Example enum:

public enum MyEnum{
    VALUE_ONE("my data");
    VALUE_TWO("some other data");

    /**
     * Used when mapping enums to database values - if that sounds odd, 
     * it is: it's legacy stuff
     *
     * set via private constructor
     */
    private String myValue; 
    //private constructor not shown

    public static MyEnum lookupEnumByString(String enumValue){
        //find the enum that corresponds to the supplied string
    }

    public String getValue(){
        return myValue;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:31:47+00:00Added an answer on May 20, 2026 at 3:31 am

    It’s all quite complicated and there may be errors, but I hope you get the idea.

    // I'm not sure about the right type arguments here
    public interface MyEnumInterface<E extends MyEnumInterface & Enum<E>> {
        public static boolean aUsefulNonStaticMethod();
        String getValue();
        MyEnumInfo<E> enumInfo();
    }
    
    /** contains some helper methods */
    public class MyEnumInfo<E extends MyEnumInterface<E>> {
        private static <E extends MyEnumInterface<E>> MyEnumInfo(Class<E> enumClass) {...}
        // static factory method
        public static <E extends MyEnumInterface<E>> MyEnumInfo<E> infoForClass(Class<E> enumClass) {
            ... return a cached value
        }
        public static <E extends MyEnumInterface<E>> MyEnumInfo(E e) {
             return infoForClass(e.getClass());
        }
        // some helper methods replacing static methods of the enum class     
        E enumForValue(String value) {....}
    }
    
    public enum MyEnum implements MyEnumInterface<MyEnum> {
        VALUE_ONE("my data");
        VALUE_TWO("some other data");
    
        private String myValue; //set via private constructor
        //private constructor not shown
    
        public boolean aUsefulNonStaticMethod(){
            //do something useful
        }
    
        public String getValue(){
            return myValue;
        }
    
        // the ONLY static method in each class
        public static MyEnumInfo<E> staticEnumInfo() {
            return MyEnumInfo.infoForClass(MyEnumClass.class);
        }
        // the non-static version of the above (may be useful or not)
        public MyEnumInfo<E> enumInfo() {
            return MyEnumInfo.infoForClass(getClass());
        }
    }
    

    It’s a bit strange, that you’re using another String in addition to Enum.name(), do you need it?

    Because of all enums extending Enum, you can’t let them share any code. The best you can do is delegating it all to a helper static method in a utility class.

    There’s no way to force classes to implement a static method, which is understandable, since there’s no way (except for reflection) to call them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.