This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 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
    
    2

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: generic question
 
Similar Threads
why not super in generic method declaration
Generics question
Doubt in generic method
question about the generic methods : ExamLab final test
Generic type declaration