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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:48:57+00:00 2026-05-24T18:48:57+00:00

Give this: Class<? extends Enum> enumClass = …; // being passed in from a

  • 0

Give this:

Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like

[unchecked] unchecked method invocation:
valueOf(java.lang.Class,java.lang.String) in java.lang.Enum is
applied to (java.lang.Class,java.lang.String)

I don’t want to use generics because that’s a major change. I don’t want to supress. I don’t understand why this warning happens. I imagine it is because one cannot extend an Enum type. I get that. But I don’t get why the wildcard class is throwing this weird error. Is there a way to fix this without using @SupressWarning or using generics?

Edit: To Clarify, the following code using generics makes the warning go away.

class Foo<T extends Enum<T>>{
    Class<T> enumClass;
    Enum e = Enum.valueOf(enumClass, aString);
}

The usage of <T> is what I mean by using generics. I can’t do that because it would be a huge cascading change.

  • 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-24T18:48:58+00:00Added an answer on May 24, 2026 at 6:48 pm

    This seems to be a compiler bug – it should be an error, not a warning.

    When compiling the method invocation expression Enum.valueOf(enumClass...), first, capture conversion is applied to the argument types.

    <W extends Enum> // a new type parameter 
    Class<W> enumClass; // the type of the argument after capture conversion
    

    Then, type inference is done for Enum.<T>valueOf(enumClass...), the result is T=W.

    Then, check the bound of T after substitution, i.e. whether W is subtype of Enum<W>.

    (this process is the same for 15.12.2.2 and 15.12.2.3; and 15.12.2.7 definitely yields T=W)

    Here, the check should fail. All compiler knows is that W is a subtype of Enum, it cannot deduce that W is a subtype of Enum<W>. (Well, we know that it’s true, barring W=Enum; but this knowledge is not present in subtyping rules, so compiler does not use it – we can verify this by playing this example with a MyEnum hierarchy, the compiler will behave the same.)

    So why does the compiler pass the bound check with just a warning? There is another rule that allows assignment from Raw to Raw<X> with an unchecked warning. Why this is allowed is another question (it shouldn’t be), but compiler does have a sense that Raw is assignable to Raw<X>. Apparently this rule is mistakenly mixed into the above subtype checking step, compiler thinks that since W is Enum, it is somehow also a Enum<W>, the compiler pass the subtype checking with just a warning, in violation of the spec.

    If such method invocation shouldn’t compile, what is the correct way? I can’t see any – as long as the type of the argument enumClass is not already in the recursive form of Class<X extends Enum<X>>, no amount of castings/conversions can make it into that form, therefore there is no way to match the signature of Enum.valueOf method. Maybe javac guys deliberately violated the spec just to make this kind of code compile!

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

Sidebar

Related Questions

I give up, how do I cast this? class AmountIsTooHighConverter : IValueConverter { public
So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>>
please give me the solution public class Calc extends Activity implements OnClickListener { private
I have this model at app/models/product.php : class Product extends AppModel { var $hasAndBelongsToMany
I have a class that extends PDO to give it the ability to pull
Given this code: class Overloading extends Object { static public void target(Object val, String
when i give this sql query in my msaccess database table called warehouse1 it
I have a script. I would like to give this script a quiet mode
Why does SQL server express 2008 give me this error? CREATE TABLE model (
Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string

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.