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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:02:24+00:00 2026-05-12T06:02:24+00:00

A co-worker ran into an interesting issue today, and while I think the actual,

  • 0

A co-worker ran into an interesting issue today, and while I think the actual, big-picture answer is “the fact that we’re having this problem means we’re doing something wrong”, I figured I’d ask this anyway.

Given the following:

public class CrazyEnumTest {

  public class EnumeratedThing<T extends Enum<T>> {
    public T myValue;

    public EnumeratedThing(T value) {
      myValue = value;
    }
  }

  public static void main (String[] args) {
    String className = args[0];
    String enumValue = args[1];

    Enum<?> value1 = Enum.valueOf(Class.forName(className), enumValue);
    EnumeratedThing<?> thing1 = new EnumeratedThing(value1);
  }
}

I get the following compile error on the call to Enum.valueOf:

Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is not applicable for the arguments (Class<capture#1-of ?>, String). The inferred type capture#1-of ? is not a valid substitute for the bounded parameter <T extends Enum<T>>

So, my question is: is it possible to, given only the String representation of a enumerated type name as well as the .name() of one of its values, get a reference to the corresponding enumerated type object as an Enum?

  • 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-12T06:02:24+00:00Added an answer on May 12, 2026 at 6:02 am

    The compile error is telling you that Class<?> is not the same as Class<? extends Enum>. Try:

    Enum<?> value1 =
            Enum.valueOf((Class<? extends Enum>) Class.forName(className), enumValue);
    

    Note that you’ll get an unchecked cast warning, so make sure that className actually does represent an enum. You could check by calling the isEnum() method on the Class object, like this:

    Class<?> enumClass = Class.forName(className);
    if (enumClass.isEnum()) {
        @SuppressWarnings("unchecked") // we just checked it, so it had better work
        Enum<?> value1 = Enum.valueOf((Class<? extends Enum>) enumClass, enumValue);
        EnumeratedThing<?> thing1 = new EnumeratedThing(value1);
    }
    

    Of course, you’ll get a raw type warning on “new EnumeratedThing(value1)” anyway.

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

Sidebar

Related Questions

I ran into an issue with some third party software that we use to
today I ran into a very interesting problem, which I was luckily able to
While working on a project I ran into the following Issue, in my CoreData
A co-worker recently ran into a situation where a query to look up security
I ran into my first compiler that changes the lvalue passed to ::delete, but
I ran into this issue when trying to add a CMFCMenuButton to an existing
I ran into a little problem today when I was creating a really quick
Why/When do you use one over the other? I just ran into an issue
I ran into an issue with an IIS web app shutting down an idle
today I ran into this problem which really bugs me, as almost the code

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.