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 8359381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:07:40+00:00 2026-06-09T11:07:40+00:00

Someone asked a question a few moments ago that got recalled because it was

  • 0

Someone asked a question a few moments ago that got recalled because it was downvoted. It was clearly noted that enums themselves cannot be generic, but the question title was “generic enum method”, and that of course is possible. The original question had some sample pseudo-Java code that went something like:

enum Test<T> {
    TEST1<T1>,
    TEST2<T2>,
    TEST3<T3>;
    // I missed some details they had here for overriding methods or such

   public T getInstance() {...
      // somehow returning a T1 when called on TEST1, a T2 for TEST2, etc.
   }
}

I am reasking, because I have a potential answer to below, to what may have been the question behind the question from @Radu.

  • 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-06-09T11:07:41+00:00Added an answer on June 9, 2026 at 11:07 am

    The sample code that had been provided (which I tried to duplicate from memory in the above question, clearly cannot work as intended, since enums themselves cannot be generic.

    But depending on what the original poster was trying to accomplish, the below may provide what was being sought, and it does compile and run.

    public enum Test {
        TEST1(String.class),
        TEST2(Object.class),
        ;
    
        Class clazz;
    
        Test(Class<?> clazz) {
            this.clazz = clazz;
        }
    
        <T> T getInstance() throws IllegalAccessException, InstantiationException {
            return (T)clazz.newInstance();
        }
    
        public static void main(String[] args) {
            try {
                String str = TEST1.getInstance();
                Object obj = TEST2.getInstance();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }
    

    There are some obvious flaws as written here. Depending on what type you are providing into each enum value, there may not be a no-arg constructor available. It could be made more complex through the use of Objenesis if needed to solve that problem. Also, I “cleverly” made the return type of the method to be a generic , so that it may be assigned without warnings/errors. However, it is totally non-typesafe to do so. You could reverse the assignments without compiler errors:

    String str = TEST2.getInstance();
    Object obj = TEST1.getInstance();
    

    but you get a runtime ClassCastException.

    In any case, I think I have proven that generic methods may be written in enums, and moreso that it is possible to write a single method in the enum that returns a new instance of differing class types, based on the type statically associated with each enum value (just not as a generic parameter of each enum value).

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

Sidebar

Related Questions

Over a year ago someone asked this question: Execute .sql files that are used
some time ago someone already asked this question and a few answers were given
I appreciate this is a question that has been asked a few times, I
Someone asked this question about a year ago but I was unable to turn
A few weeks ago i asked a question about a multilanguage site (see Multilanguage
Someone asked the question: INT, BIGINT or UUID/GUID in Oracle, DB2, Derby and HSQLDB?
I found this on Google, click here , which someone asked a similar question,
Someone has asked the exact same question in April, without any answer. But since
This question was asked earlier by someone else but never answered: MKAnnotation - Map
Fairly straightforward question here, but I can't seem to find someone who has asked

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.