| Author |
Why generics work in that strange way?
|
Lukas Stephienn
Ranch Hand
Joined: Dec 23, 2005
Posts: 60
|
|
Hi, I spotted some strange thing in generics. This code works fine, method automatically returns what it should: Strange is that following code does not compile: takeList method does not accept argument returned by newArrayList(). So, the question is why this generic method in assignment works fine, while putting it as a method argument fails? Thanks in advance. [ October 31, 2008: Message edited by: Lukas Stephienn ]
|
SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
Because you are not supplying any information to the compiler about what <T> to use.
|
 |
Lukas Stephienn
Ranch Hand
Joined: Dec 23, 2005
Posts: 60
|
|
But in both cases in wrote that I want to have List<String>. On left side of assignment operator in first case. In argument in second case.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26168
|
|
Looking at this in isolation, how can you tell that a String is desired? There's nothing a caller can pass to indicate this fact. And the compiler can't go by what you are storing the result in.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Charles Lyons
Author
Ranch Hand
Joined: Mar 27, 2003
Posts: 836
|
|
You would really want to do this (if indeed you would really want to do this):Then invoke with:You're now telling the compiler at compile-time what type T is (String in fact). Good for demonstration purposes, pointless in production. [ October 31, 2008: Message edited by: Charles Lyons ]
|
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Or just use the intermediate variable:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
But the last 3 words of Charles Lyon's post said it all.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Originally posted by Charles Lyons: You would really want to do this (if indeed you would really want to do this):Then invoke with:
Why would this be more desirable than the OP's original code? [ October 31, 2008: Message edited by: Garrett Rowe ]
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Charles Lyons
Author
Ranch Hand
Joined: Mar 27, 2003
Posts: 836
|
|
Why would this be more desirable than the OP's original code?
Perhaps because the OP explictly wanted to do:in the second example, without an intervening local variable? As an exercise in using generics it's good as well: there are cases where passing a Class as an argument to determine the type of a generic method is useful. Though as we ascertained, neither are better than:
|
 |
Marcin Mogiela
Greenhorn
Joined: Aug 30, 2007
Posts: 14
|
|
"Effective Java second edition" by Joshua Bloch writes that one of the advantages of static factory methods is that they reduce the verbosity of creating parametrized type instances. Such as: and a wordy declaration: could be replaced by more succinct alternative. Example: I think the question is not the usefulness of such practices, but why it is impossible to use HashMap.getInstance() method invocation as an argument to a method. Such method does not really exist but suppose it does. The example comes from the book. [ November 03, 2008: Message edited by: Marcin Mogiela ]
|
SCJP 5.0, SCWCD 1.4
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Why generics work in that strange way?
|
|
|