• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Understanding effectively final variable in Java

 
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I have tried and executed a program using method references but got confused in understanding effectively final variable in java in the following code :



If str is not effectively final, how does the program compiled and ran with output.
 
Marshal
Posts: 5654
329
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think line 7 String str ="ABC"; is an "effectively final" variable?
 
Saloon Keeper
Posts: 3946
43
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is not a lambda expression but rather a method reference. And they have indeed different restrictions.

While lambda cannot use non-final local variables, method reference has no such limitation.

Please check the details here: https://stackoverflow.com/questions/33052917/why-can-method-reference-use-non-final-variables
 
Swapna latha
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for posting the stackoverflow link. I understood now.

Mikalai Zaikin wrote:Your code is not a lambda expression but rather a method reference. And they have indeed different restrictions.

While lambda cannot use non-final local variables, method reference has no such limitation.

Please check the details here: https://stackoverflow.com/questions/33052917/why-can-method-reference-use-non-final-variables

 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mikalai is quite correct that the central issue in your question is answered by the fact that you are not, in fact, using lambdas here. You are using method references, and they have different rules.  They have no problem with variables being final or effectively final.

The stack overflow link refers in several places to "final variables" when it should really talk about effectively final variables.  So be careful when reading it; some of the statements may be misleading.

Your code example does not have any effectively final variables.  The variable on line 7 could have been effectively final, if you had not added code on lines 8 and 12 which changes it.  Once you do that, the variable is no effectively final anywhere, even at line 7.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic