Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not related to generics. Switch statement for enum in java can only use values of that particular enum, thus it's <em>prohibited</em> to actually specify enum name. This should work: </p> <pre><code>switch(event.getId()) { case SELECTED: l.selected(event); break; case SELECTION_CLEARED: l.unselect(event); break; } </code></pre> <p><strong>Update</strong>: Ok, here's an actual code (which I had to change a little bit to get it to compile with no dependencies) that I've copy / pasted, compiled and ran - no errors: </p> <p>AbstractEvent.java</p> <pre><code>public abstract class AbstractEvent&lt;S, T extends Enum&lt;T&gt;&gt; { private S src; private T id; public AbstractEvent(S src, T id) { this.src = src; this.id = id; } public S getSource() { return src; } public T getId() { return id; } } </code></pre> <p>MyEvent.java </p> <pre><code>public class MyEvent extends AbstractEvent&lt;String, MyEvent.Type&gt; { public enum Type { SELECTED, SELECTION_CLEARED }; public MyEvent(String src, Type t) { super(src, t); } } </code></pre> <p>Test.java </p> <pre><code>public class Test { public static void main(String[] args) { fireEvent(new MyEvent("MyClass.myMethod", MyEvent.Type.SELECTED)); } private static void fireEvent(MyEvent event) { switch(event.getId()) { case SELECTED: System.out.println("SELECTED"); break; case SELECTION_CLEARED: System.out.println("UNSELECTED"); break; } } } </code></pre> <p>This compiles and runs under Java 1.5 just fine. What am I missing here?</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