| Author |
generic question
|
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
why?
how could i return E?
|
 |
Horia Constantin
Greenhorn
Joined: Sep 23, 2009
Posts: 22
|
|
Well, I can say from start that that code will never compile because you cannot have 2 returns like that (one after another). It will not compile (and probably say statement unreachable).
The question that arises... Didn't you check the error given by the compiler at compile time? That should have been pretty obvious.
Next time pay more attention to what the compiler has to say (questions like these are to be expected at the exam).
Cheers,
Horia
|
For every winner, there are dozens of losers. Chances are you're one of them.
|
 |
Allan Cheong
Ranch Hand
Joined: Nov 12, 2009
Posts: 68
|
|
|
I don't think you can return E. E is a generic type. You can only return an object of type reference B.
|
SCJP6, SCJD, SCWCD5
Perspective
Freaky Codes
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Yes E is a generic type. At runtime, it actually doesn't denotes anything, so you can't instantiate an object of type E. You'll have to create an instance of an actual type (class or interface) that is compatible with E. Since here E here is <E extends B>, so you can return an instance of B or a subclass of B...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
Ankit Garg wrote:Yes E is a generic type. At runtime, it actually doesn't denotes anything, so you can't instantiate an object of type E. You'll have to create an instance of an actual type (class or interface) that is compatible with E. Since here E here is <E extends B>, so you can return an instance of B or a subclass of B...
thank you
|
 |
 |
|
|
subject: generic question
|
|
|