This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic question" Watch "generic question" New topic
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: 71

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, OCJWCD5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic question
 
Similar Threads
Doubt in generic method
why not super in generic method declaration
Generics question
Generic type declaration
question about the generic methods : ExamLab final test