File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Method in K&B Question 14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Method in K&B Question 14" Watch "Generic Method in K&B Question 14" New topic
Author

Generic Method in K&B Question 14

Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
In playing with Question 14 in K&B I realized I'm still pretty fuzzy on the Generics Stuff. I've read other threads that explain in detail but something is not quite sinking in. Question:

Why does this compile:



But this does not:
Julio Eneriz
Greenhorn

Joined: Nov 28, 2007
Posts: 15
I think the problem is that you are defining twice E. This compiles:

public static <E extends CharSequence> Collection<E>
getLongWordsC(Collection<E> col){
return new ArrayList<E>();
}

The one with ? compiles because it's something different.
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
I think the problem is that you are defining twice E.


This also does not compile with the same error as the other, a squiggly on extends saying to remove it and a message that the return type is missing:

Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
Same thing here:



That's just to note that its not complaining that the value being returned is of the wrong type but that the return type is not declared correctly.

So why is it not correct?
Julio Eneriz
Greenhorn

Joined: Nov 28, 2007
Posts: 15
The problem now is that you are trying to use T without declaring it. Or, if Collection<T extends CharSequence> was a declaration (which is not), then the returning type is missing.

Besides, you cannot make a new Collection, because Collection is an interface.

I think what you are trying would be this:

public static <E extends CharSequence, T extends CharSequence> Collection<T> test(Collection<T> col){
return new ArrayList<T>();
}
[ November 30, 2007: Message edited by: Julio Eneriz ]
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
Starting to get clearer. So is there ever a case where you could see

<E extends Anything>

as part of the return type?
Julio Eneriz
Greenhorn

Joined: Nov 28, 2007
Posts: 15
Originally posted by Karen Marie:
Starting to get clearer. So is there ever a case where you could see

<E extends Anything>

as part of the return type?


<E extends Anything> is not a valid return type, is the definition of what is E. Once defined, you can use it, something like that

<E extends Anything> E getThat() {...}

Or



I cannot find any useful use for that. I think the use of generics in return type happens when you define E in the class (for example class Draw<E extends Clothes> and something like <E> getFirstItem ).

Maybe someone with more experiencie will do it.
 
jQuery in Action, 2nd edition
 
subject: Generic Method in K&B Question 14