This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Wildcard character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wildcard character" Watch "Wildcard character" New topic
Author

Wildcard character

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
I don't understand, why the following block of code giving the compile time error:

T means anytype, so why despite of passing the argument like it's still giving the compile time error?
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Originally posted by Pawan Arora:
I don't understand, why the following block of code giving the compile time error:

T means anytype, so why despite of passing the argument like it's still giving the compile time error?



No, T is just a template variable. [And you are using it twice as return value...]<-- This was wrong. You CAN declare it like that and then it will take the T from the parameter sent into it...

If I was to create a class of the type that the method resigns in using a String as T the generated method would look like this:



That is okey.

If I use an Integer, the code would translate to:



UPPS!! You can't return a List of Strings when I want you to return a list of Integers.

[ October 16, 2008: Message edited by: Ove Lindstr�m ]
[ October 16, 2008: Message edited by: Ove Lindstr�m ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You either want to return an ArrayList<T>, or change the method signature to remove T and just return List<String>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wildcard character
 
Similar Threads
Mock Question on Generics
Genrecis and Wildcard
Generics Doubt
generics
Genericss