• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generic methods doubt # 2

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, ranchers! Please explain me why first code compiles fine, but second generates compile time error? What is the main idea on this?



 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get an error in the line you mention.

I get an error in the call to testGenericMethod(), but i don't know why your getting this error
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also getting the compilation error on the line that calls testGenericMethod(). But don't know why.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the error you get?
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the compile error:

The method testGenericMethod(Collection<T>, T) in the type GenericTest is not applicable for the arguments (List<capture-of ? extends Object>, Object)

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most interessant is that if you pass only one argument, the code compiles...
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andry Dub:
Hi, ranchers! Please explain me why first code compiles fine, but second generates compile time error? What is the main idea on this?



In the first code, you have a collection of instances that are base classes of Object (which can only be Object) and an Object instance. There is a type that matches the method that you defined (Object).

In the second code, you have a collection of instances that are sub classes of Object (which can be anything) and an Object instance. There isn't a type that matches the method that you defined.

The most interessant is that if you pass only one argument, the code compiles...



It would be more interesting... if you show us this code that compiles.

Henry
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiles:


or if you uses distinct generics, compiles too.


[ December 16, 2007: Message edited by: Alexsandra Carvalho ]
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh... That is what you meant by using one parameter. I thought you meant calling it with one parameter -- but keeping the method the same. Now that would have been interesting!


As for these two examples, well, they work because the compiler can find a type (or two types) that matches... which was not true in the original example.

Henry
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To further clarify, the reason there is a compile error, is because the compiler can't find a type that matches both parameters. It doesn't mean that the collection is invalid, as you can see by Alexsandra's examples.

Another option to get it to compile is by extracting the value from the collection itself -- hence, it is guarantee to match. (Of course, since the collection is empty, this will generate a runtime exception)



Henry
[ December 16, 2007: Message edited by: Henry Wong ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic