• 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

Can someone explain this behavior

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm hoping one of the resident Java experts here can explain this to me.

I've been studying for my OCP certification and I've come across something I don't understand.  

Note:  the code below is non-sense and not meant to do anything real but rather to illustrate the behavior that I'm curious about.  I would never write production code like this but I can't help but be puzzled as to why it works this way.

The following code compiles w/o issue



if I move the final two lines of that block above the predicate definition it doesn't compile and it instead complains that I'm attempting to redeclare c & start inside my predicate.



I assume this is something about how Java deals with lambda methods but why does having the declarations for c & start before the predicate cause issues while having them below does not?

 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

This has nothing to do with lambda's specifically, but rather Java's rules regarding variable scopes in general.

You are not allowed to declare a local variable when another local variable with the same name is currently in scope.

In your first code snippet, the variable c is in scope for the duration of the lambda body, and then goes out of scope again after line 6. At line 7, there is no local variable with the name c in scope, so the declaration of the variable c is legal.

In your second code snippet, the variable c is already in scope at line 3, and then remains for the remainder of the method body. That means you can't declare a second variable c at line 4: there is already a variable named c in scope.

Here is an even simpler example that demonstrates the issue, without the use of lambda expressions:

 
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred,

This is not about lambdas, this is about a scope of variable. A variable cannot be redefined in the same scope. After the lambda expression, the variable is no longer in scope and it can be defined again.
This will compile too:

But if i is declared before the for loop, the compilation will fail because the for loop will try to redeclare i.
You can read about scopes of different declarations here.

6.3. Scope of a Declaration wrote:The scope of a local variable declared in a block by a local variable declaration statement (ยง14.4.2) is the rest of the block, starting with the declaration's own initializer and including any further declarators to the right in the local variable declaration statement.


 
Fred Hildebrandt
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan & Ira:

Thanks for explaining this to me.  

-Fred
 
It will give me the powers of the gods. Not bad for a 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