• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • 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:

 
Paweł Baczyński
Saloon Keeper
Posts: 1955
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 1955
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 1955
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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 ...
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!