2

I'm quiet new into generics in java. I have an Interface called RIEngine.

I was trying following example:

private <T> boolean allExist(List<T extends RIEngine> resultedList, 
                             String... columnName)
{ ... }

doesn't compile.

But

private <T> boolean allExist(List<? extends RIEngine> resultedList, 
                            String... columnName)
{ ... }

compiles.

My question is: why is this so.

Why can't I use type = "T" instead of wild card?

Please help me to understand.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Hasan
  • 299
  • 3
  • 6
  • 16
  • Please show a minimal example that doesn't compile. – Oliver Charlesworth May 23 '12 at 14:06
  • Sun has a really nice tutorial on that: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html. Also, is exact duplicate of http://stackoverflow.com/questions/3486689/java-bounded-wildcards-or-bounded-type-parameter – Nikola Yovchev May 23 '12 at 14:07
  • by the way, in your second example, you are not using `T` at all. So `private boolean allExist(List extends RIEngine> resultedList, String... columnName)` is sufficient. If the inside of your method doesn't need to use `T`, then using a wildcard without `T` is simpler. – newacct May 23 '12 at 17:59

1 Answers1

9

Try this:

private <T extends RIEngine> boolean allExist(List<T> resultedList, String... columnName){...

You need to put your type bounds in the type declaration, not where it's used.

The reason the second one compiles is because it's a wildcard (which you can use without previously declaring as a type for the method)

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • +1 for being 27 seconds faster than me :) and please accept this answer. I just noticed it after my post was sent. – Stephan May 24 '12 at 07:26