• 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

how does lambda map to functional interface in different locations ?

 
Ranch Hand
Posts: 170
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I define a functional interface in my class like



Then inside this class, when I do (int a, int b) -> a +b;  it knows it should match "MathOperation" interface since that's the only one that matches.   However, if the above interface code is NOT in the same class as (int a, int b) -> a +b;
how does it know which one should be ?  For example , if I define some interfaces like


Then in my code

 
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
Linwood,
Great question. You don't just define a lambda in isolation. Instead you assign it to a variable or pass it as it to a method as a parameter. This means the compiler has context to figure out which interface you are talking about.

In other words, the compiler uses the context to figure out what it should be.
 
Linwood Hayes
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne.  So I can do SecondClass.MathOperation op = (a,b)->....
or
ThridClass.MathOperation op2 = (a,b)->....

right ?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic