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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:42:58+00:00 2026-06-14T19:42:58+00:00

Consider that I have an interface com.mycompany.SomeInterface , and an enum com.mycompany.SomeEnum implements SomeInterface

  • 0

Consider that I have an interface com.mycompany.SomeInterface, and an enum com.mycompany.SomeEnum implements SomeInterface. I want to get all enum constants of this class – as instances of SomeInterface – at runtime using the Reflection API.

Currently, my code (in the class EnumConstantGetter) is something like this:

Class<?> clazz = EnumConstantGetter.class.getClassLoader().
  loadClass("com.mycompany.SomeEnum");

if (!(SomeInterface.class.isAssignableFrom(clazz)) {
    throw new Exception("the class doesn't implement SomeInterface");
}

if (!(clazz.isEnum()) {
    throw new Exception("not an enum");
} 

Class<? extends Enum<? extends SomeInterface>> castClass =
  (Class<? extends Enum<? extends SomeInterface>>) clazz; // cast #1
ArrayList<SomeInterface> vals = new ArrayList<SomeInterface>();
for (Enum<? extends SomeInterface> enumConstant :
  castClass.getEnumConstants()) {
    vals.add((SomeInterface) enumConstant); // cast #2
}

The above code appears to work, but I get a compiler warning at the creation of castClass.

My question, then, is: are both casts noted in the code (the cast on the class and the cast on the constant) necessarily valid based on my checks?

In other words, is every member of Enum<? extends T> guaranteed to implement T?

If the answer is, “Yes, the casts are safe,” then why does the compiler give me this warning?

If not, why not? or, in what circumstances could the routine fail?

EDIT: As my code above is apparently confusing, here’s my explanation of what’s supposed to be happening:

  1. Load the class named SomeEnum in the package com.mycompany and store its Class object in a variable.
  2. Ensure that the referenced class implements the SomeInterface interface.
  3. Ensure that the referenced class is an enum.
  4. As we know that it’s an enum implementing SomeInterface, cast it to a Class<? extends Enum<? extends SomeInterface>> – cast #1
  5. Loop through all the enum constants.
  6. For each constant, cast it to a SomeInterface – cast #2 – and add it to the list of constants.

Thanks!

  • 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-14T19:42:59+00:00Added an answer on June 14, 2026 at 7:42 pm

    You could do the following:

    Class<?> clazz = ...;
    Class<? extends SomeInterface> someInterfaceClass;
    try {
        someInterfaceClass = clazz.asSubclass(SomeInterface.class);
    }
    catch (ClassCastException cce) {
        throw new IllegalStateException("Specified type doesn't implement SomeInterface", cce);
    }
    SomeInterface[] enumConstants = someInterfaceClass.getEnumConstants();
    if (enumConstants == null) {
        throw new IllegalStateException("Specified type is not an enum.");
    }
    
    //use constants
    

    This avoids the warnings because asSubclass is checked and getEnumConstants returns null if the Class object “does not represent an enum type”.

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

Sidebar

Related Questions

Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo
Suppose I have interface and implementation class that implements it and I want to
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
Consider that I have an interface that contains the following property interface IFoo {
Consider an interface that has only one method: Object getValue(String data); Suppose this describes
Consider the following. You have a class that you want to serialize with XmlSerializer
Consider that we have a DIV with fixed height. Without a defined width ,
Consider that I have 1 resource and 2 urls (let's say new one and
I have a chat application(Socket Programming) , for that consider I have 2 separate
Consider the apps that large blogs have (which work pretty much like an RSS

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.