• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Method Reference for Generic class

 
Ranch Hand
Posts: 73
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read a Java's book.

The author states that when a generic method is specified as a method reference, its type argument comes after the :: and before the method name.

It demonstrates a way to specify a method reference to a generic method.

See code below. I understand this approach. And the code works perfectly fine.




However, he also mentioned about in cases which a generic class is specified, the type argument follows the class name and precedes the ::
I tried something like this, but it didn't work.

 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first code block shows the correct way to declare a generic method. In the second block, T counts as an instance feature of the class, and as you know static parts of the class cannot access instance features. So the second code block won't compile.
You hardly ever need to declare the method reference as generic. I have only seen that done when it is necessary to disambiguate an object from an array.
Names like mRef for a test object are a good recipe for future confusion.
 
henry leu
Ranch Hand
Posts: 73
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Could you kindly provide an example showing method referencing the generic class?

I know it is not used often. But as a Java enthusiast, I would like to see how it works.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but there is an example in the Java™ Tutorials.
 
henry leu
Ranch Hand
Posts: 73
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I worked something out.

I demonstrate the lambda expression, and also
method referencing (when generic class and generic method are specified). See line 5.

This is a working code. This code does logically nothing. But I just try to show what I wanted to know from the author's book.

Please advise if this is the correct way to use method reference for generic class and generic method.

When a generic method is specified as a method reference, its type argument comes after the :: and before the method name.
When a generic class is specified as a method reference, its type argument follows the class name and precedes the :: .

 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

henry leu wrote:. . . Please advise if this is the correct way to use method reference for generic class and generic method. . . .

Probably not; I expect that code would run exactly as before with the <XYZ> types removed.
 
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Car::move can be used as well, for instance:
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic