• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Accessing enclosing local variable from the body of a method reference.

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I access a local variable present in the calling method from a method reference?




The following seem to compile fine:

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't access a local variable from another method.
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So a method reference has limitations when compared with a lambda expression?
That is to say that a lambda expression is more flexible than a method reference??
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable ingredient inside a lambda and the argument variable ingredient of mixIngredient method are unrelated.
They are different variables. The fact that they have the same name is irrelevant.
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh, I was asking about the variable

It is accessible inside a lambda, but I see no way of accessing it inside the method reference ...
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I misunderstood your question.

You can replacewithand this can be refactored to use a method reference
By the way, you have some problems in your code.
- public static void makeCake(List<Ingredients> ingredients) should be public static void makeCake(List<Ingredient> ingredients)
- Stream doesn't have sort() method. It has sorted().
- Stream doesn't have foreach() method. It has forEach().
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. That worked for the example I gave, but the example I gave was something I made up.

What I really want is to access a local variable while iterating a collection.
For example let us say that I have an excel worksheet object and a list object,
for each internal iteration of the list object, I want to add an excel row to the worksheet with the data extracted from the list object for that particular iteration.

I don't want to post the code here as it is kinda long ... that is why I made up the code ...
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to use a local variable in a lambda, that variable has to be in scope (and effectively final, I believe). Local variables in completely separate methods are not in each other's scope, so they can't reference each other. This idea of separate scopes is captured by Pawel's earlier post:

Paweł Baczyński wrote:The variable ingredient inside a lambda and the argument variable ingredient of mixIngredient method are unrelated.
They are different variables. The fact that they have the same name is irrelevant.

 
Sheriff
Posts: 22662
127
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

Jason Bullers wrote:If you're going to use a local variable in a lambda, that variable has to be in scope (and effectively final, I believe).


Both statements are correct.
 
reply
    Bookmark Topic Watch Topic
  • New Topic