File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Help with clarity on this Generics question
Joined: Aug 16, 2007
Nov 14, 2008 00:57:00
This question if from a Mock exam by John Meyers.
Can you please clariry why the correct answer says the Line marked // 2 will not Compile and the return type of this method is List<T>.
My understanding was the code would compile jus fine.
public <T> List<T> meth(List<?> type)
System.out.println(type); // 1
>(); // 2
Thanks in advance.
[ November 14, 2008: Message edited by: Siphiwe Madi ]
Regards, Siphiwe Madi
[SCJP, SCWCD, __] Next ...
Joined: Aug 03, 2008
Nov 14, 2008 01:19:00
Siphiwe the code will not compile. The method itself cannot decide what is the type of T. It returns a List of type T. But the method itself doesn't know the type of T. It depends on the calling code.
If the call is
List<Integer> l = obj.meth(new List<String>());
then the type of T will resolve to Integer. And if the call is
List<String> l = obj.meth(new List<Object>());
then the type of T will resolve to String...
SCJP 6 | SCWCD 5 |
Javaranch SCJP FAQ
subject: Help with clarity on this Generics question
Mock Question on Generics
John Meyers's SCJP mock errata
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014