Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

! Generic Methods Calling is not invoking the right method  RSS feed

 
Greenhorn
Posts: 3
Java MySQL Database Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

I'm Confused with specifically calling Generic Method. If we call generic method like JenericMethod.<String[]>ship(argument1), it is suppose to call the method that its parameter is array. However in this case, it is calling the overloaded function that doesn't have Array as a parameter. What am I missing here?





 
author & internet detective
Posts: 39054
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good question.

JenericMethod.ship(argument1); also calls the array version.

So the generic is resolving to T. I don't know why other than "that's how the compiler works."

In general, it isn't a good idea to do generics like this of course.
 
Marshal
Posts: 62172
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that some sort of resoltion of overloading question. A T[] is more specific than a T, so the compiler chooses the T[] version. What happens if you try this change?
 
Campbell Ritchie
Marshal
Posts: 62172
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:Isn't that some sort of resolution of overloading question. A T[] is more specific than a T, so the compiler chooses the T[] version. What happens if you try this change?

That resolution things might explain the first call, but if you say that T means String[], you will get the non‑array version of the method.
 
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is right. Both of your methods have a generic argument T. When you use JenericMethod.<String[]>ship, you use String[] for T, not for T[] - because the generic type is not T[] but T.
 
Salemikael Kebede
Greenhorn
Posts: 3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Campbell is right. Both of your methods have a generic argument T.


One of the method has T and the other T[] as argument/parameter.

Rob Spoor wrote:When you use JenericMethod.<String[]>ship, you use String[] for T, not for T[] - because the generic type is not T[] but T.



I don't think it is possible to declare generic type as T[], And JenericMethod.<String>ship calls the overloaded array method while the generic type is T.  The confusing thing for me is that when I use JenericMethod.<String>ship to call the method, the compiler identifies correctly which method to call based on the argument.

As you can see in the image below, the IDE also identifies the correct/ my assumption/  method using the argument type.
Array_argument.jpg
[Thumbnail for Array_argument.jpg]
nonArray_argument.jpg
[Thumbnail for nonArray_argument.jpg]
 
Rob Spoor
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Salemikael Kebede wrote:

Rob Spoor wrote:Campbell is right. Both of your methods have a generic argument T.


One of the method has T and the other T[] as argument/parameter.


The method parameters are indeed T and T[]. But the generic argument is the part between < and >, and that is T for both.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!