1

I am trying to call the Enum<E> class's static method valueOf() but I received a compile error. Please look at the code snippet below.

public void hello(Class<? extends Enum<?>> q){  
    Object o= Enum.valueOf(q,"hello");      
}

IntelliJ IDEA complies the following code just fine, but Eclipse gives a compile error:

enter image description here

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Raj
  • 203
  • 1
  • 2
  • 8
  • Please check now , I have edited it--that was a typing error. – Raj Dec 29 '15 at 18:07
  • 1
    Create [MCVE](http://stackoverflow.com/help/mcve) I cannot understand your question. – StackFlowed Dec 29 '15 at 18:09
  • Did you put this code in you IDE , and check ? I am getting compile error in the line where I call the .valueOf() method. – Raj Dec 29 '15 at 18:10
  • Let me put a screenshot here... – Raj Dec 29 '15 at 18:12
  • jason Z: I just added a picture of the error I am getting , next to my question.Please have a look. – Raj Dec 29 '15 at 18:15
  • Java 1.7 : I just pasted the same code in IntelliJ and no compile error ; but In eclipse it gives me an error. Is it a bug with Eclipse? – Raj Dec 29 '15 at 18:19
  • Just try to run the code in IJ and you will get the same error that eclipse shows at compile time , so this means that the code has some error in it ; which brings us to original question as to why this code is erroneous? – Raj Dec 29 '15 at 18:41
  • @StackFlowed The question contains an MCVE if you care to put the method in a class. – user1803551 Dec 29 '15 at 20:28

1 Answers1

1

I don't know IntelliJ's workings, but I can tell you the reason why it doesn't compile.

The method defined in the class Enum<T extends Enum<T>>

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

has a first argument that is the class of a type T which is defined by T extends Enum<T>. That is, it extends an enumeration type with the same generic type as itself.

When you call the method with the first argument as

Class<? extends Enum<?>>

you are not specifying that the two ? are in fact the same type, as required. This throws the mismatch error.

What you can do with your method is turn it generic:

public static <E extends Enum<?>> void hello(Class<E> q)

but as you can imagine, this will still not compile because ? is not necessarily E. So you need to spell it out:

public static <E extends Enum<E>> void hello(Class<E> q)

and this will compile.

Edit: another way to think about it

You could think of fixing your method signature

public void hello(Class<? extends Enum<?>> q)

by changing it to

public void hello(Class<E extends Enum<E>> q)

just to specify that the two wildcards are the same. The concept is right, but the syntax is not legal. You must define what E is (either by making your method generic or your class).

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • But ONLY an enum class Test1 can extend Enum. So ? extends Enum> ; will have ? values which will be the same. Which means when you call valueOf() static method , you will pass the variable T such that T will extend Enum. – Raj Dec 29 '15 at 19:02
  • @Raj I don't think the compiler can know this during type erasure. The unbounded `?` is replaced with `Object` while `? extends X` is replaced with `X`. – user1803551 Dec 29 '15 at 20:06