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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:35:58+00:00 2026-06-11T07:35:58+00:00

i would like to get rid of these warnings about unchecked conversion and parameterization

  • 0

i would like to get rid of these warnings about unchecked conversion and parameterization without surpressing them.

interface Switch {
    void toggle();
}
enum A implements Switch {
    a1,a2;
    @Override public void toggle() {
        state=!state;
    }
    boolean state;
}
enum B implements Switch {
    b1,b2;
    @Override public void toggle() {
        state=!state;
    }
    boolean state;
}
public class Warnings {
    public static void main(String[] args) {
        Class<? extends Enum>[] enums=new Class[]{A.class,B.class};
        for(Class<? extends Enum> clazz:enums)
            try {
                Enum s=Enum.valueOf(clazz,args[0]);
                ((Switch)s).toggle();
            } catch(IllegalArgumentException eee) {}
    }
}
  • 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-11T07:35:59+00:00Added an answer on June 11, 2026 at 7:35 am

    You can’t without writing your own valueOf. Enum is defined as:

    class Enum<E extends Enum<E>>
    

    and Enum.valueOf is defined as:

    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                                String name) 
    

    Note the recursive type parameterization which implies that you can only call valueOf with a specific enum class (e.g. A.class), but not with a generic one, as a Class<? extends Enum<?>> is not a match because the two question marks aren’t assumed to represent the same (unknown) type by the compiler.

    So apart from using generic collections instead of arrays, you have to write your own valueOf method that accepts any enum class.

    public class Warnings {
        public static void main(final String[] args) {
            List<Class<? extends Enum<?>>> enums = new ArrayList<Class<? extends Enum<?>>>();
            enums.add(A.class);
            enums.add(B.class);
            for (Class<? extends Enum<?>> clazz : enums) {
                try {
                    Switch s = valueOf(clazz, args[0]);
                    s.toggle();
                } catch (IllegalArgumentException eee) {
                }
            }
        }
    
        private static Switch valueOf(final Class<? extends Enum<?>> enumClass, final String name) {
            Enum<?>[] enumConstants = enumClass.getEnumConstants();
            for (Enum<?> constant : enumConstants) {
                if (constant.name().equals(name)) {
                    return (Switch) constant;
                }
            }
            throw new IllegalArgumentException(name + " is not a constant of enum class " + enumClass.getName());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get rid of the close/X button on the top right
In a UITextField used for entering decimal numbers I would like to get rid
I would like to know if there is a way to get rid of
I would like to get numbers of comments for each posts using mysql. I
I would like to get to know how to redirect output of some program
I would like to get View instance that is used to display specific Preference
I would like to get the generic information ( Counter class) of my CounterPersistence
I would like to get a help from you all. The below is the
I would like to get started developing games with Flixel, but im on a
I would like to get the weighted_median of an unsorted, variable length, eigen c++

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.