Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question regarding method reference

 
Ranch Hand
Posts: 163
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am reading javatpoint.com blog and i came across method references and got stuck at one point.



I am not sure about the line : Sayable sayable = MethodReference::saySomething;
How MethodReference::saySomething is assigned to an interface as MethodReference has not implemented the interface.

Thanks & Regards,

Swapna.
 
Enthuware Software Support
Posts: 4646
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MethodReference need not implement Sayable. Compiler will automatically create and instantiate an anonymous class that implements Sayble. Compiler will also implement the say method of that anonymous class on its own. Inside the method, it will put a call to MethodReference's saySomething.  So, the compiler will translate this line of code Sayable sayable = MethodReference::saySomething; to :


Sayable sayable = new Sayable(){
                                  public void say(){
                                        MethodReference.saySomething();
                                   }  };

 
Swapna latha
Ranch Hand
Posts: 163
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:MethodReference need not implement Sayable. Compiler will automatically create and instantiate an anonymous class that implements Sayble. Compiler will also implement the say method of that anonymous class on its own. Inside the method, it will put a call to MethodReference's saySomething.  So, the compiler will translate this line of code Sayable sayable = MethodReference::saySomething; to :


Sayable sayable = new Sayable(){
                                  public void say(){
                                        MethodReference.saySomething();
                                   }  };



Thank you Paul
 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:. . . an anonymous class that implements Sayble. . . .

If I write an anonymous class the old way, I get a class file called CampbellsClass$1.class or similar. I have never seen such a file if I use a λ, but you can see this sort of thing on JShell

Comparator<String> stringLengthComparator = Comparator.comparingInt(String::length);
stringLengthComparator ==> java.util.Comparator$$Lambda$26/0x0000000800b6b840@68de145

If I do it from the terminal I get this sort of thing:

[critchie@localhost java]$ javac AnonymousClassDemo.java
[critchie@localhost java]$ ls -a
.  ..  AnonymousClassDemo.class  AnonymousClassDemo.java

No ...$xyz.class or ...$$xyz.class to be seen anywhere. If I change it to a classical anonymous class, you can see the difference:-

[critchie@localhost java]$ javac AnonymousClassDemo.java
[critchie@localhost java]$ ls -a
.   'AnonymousClassDemo$1.class'   AnonymousClassDemo.java
..   AnonymousClassDemo.class

You can't see the two compiler errors beause I misspelt @Override and I forgot I had to write new Comparator<>() rather than new Comparator(). Another goal scored by method references
 
Paul Anilprem
Enthuware Software Support
Posts: 4646
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Anilprem wrote:. . . an anonymous class that implements Sayble. . . .

If I write an anonymous class the old way, I get a class file called CampbellsClass$1.class or similar. I have never seen such a file if I use a λ, but you can see this sort of thing on JShell



That's a good point. JLS 11 Section 15.27.4 "Run-Time Evaluation of Lambda Expressions" , says, among other things:

...
The value of a lambda expression is a reference to an instance of a class with the following properties:
...


So, even though a lambda expression is converted to an instance of a class, it may not be the compiler that does this conversion!
 
Campbell Ritchie
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:. . . JLS 11 Section 15.27.4 "Run-Time Evaluation of Lambda Expressions" , says, among other things:

...
The value of a lambda expression is a reference to an instance of a class with the following properties:
...


So, even though a lambda expression is converted to an instance of a class, it may not be the compiler that does this conversion!

Thank you I don't seem to have read that JLS section. As you noticed. it doesn't say whether the class is constructed at compile time or class loading time or runtime. It also says, if you get two identical instances of λs, they might be interned rather like Strings.
 
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic