• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

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: 39172
724
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: 63461
207
  • 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: 63461
207
  • 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: 21649
101
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: 21649
101
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!