• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Methods Calling is not invoking the right method

 
Greenhorn
Posts: 4
Oracle MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic