Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to declare a Class<?> object such that is is an Enum AND an Interface in Java
    text
    copied!<p>I have a utility class that needs to work on a generic Class but must be restricted to those that are an enum and implement a particular interface.</p> <pre><code>// These two work Class&lt;? extends Enum&lt;?&gt;&gt; enumClass; Class&lt;? extends MyInterface&gt; interfaceClass; // This is what I want but does not work Class&lt;? extends MyInterface &amp; Enum&lt;?&gt;&gt; enumAndInterfaceClass; </code></pre> <p>For generics I can successfully use this</p> <pre><code>public class MyClass&lt;T extends Enum&lt;T&gt; &amp; MyInterface&gt; { public MyClass(Class&lt;T&gt; theClass) { ... } } </code></pre> <p>However I can't use the Generics everywhere and need to declare it separately. I am not sure if this is even possible. <br/> So my question boils down to how can I declare a member variable with those constraints? </p> <p>So Currently MyClass is a singleton then as needed the enum/interface can be updated. The return values of its operations will change depending on which enum it is given. I would like to not have the generics on it since that would require creating a new instance for every change to the enum. There is a lot of code using it already so deviating from the singleton is not going to be approved. So a reference must be kept. I suppose I could only enforce the Interface requirement then check in the setter method that it is an enum throwing an exception otherwise but that is not ideal. </p> <p><strong>Edit</strong> (Updated question and added more detail)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload