• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Generic methods doubt # 2  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the error you get?
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
The most interessant is that if you pass only one argument, the code compiles...
 
author
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!