• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

OCP Java SE 11 Programmer II Study Guide (Sybex 816) Errata Chapter 1

 
Ranch Foreman
Posts: 650
14
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I let this slide when I was rushing thru the first time, as a possible minor detail, but with more time to reflect I think that it is likely enough to cause confusion that I am bringing attention to it now.

In the last section of the first Chapter, just before the summary, "Variables Referenced from the Lambda Body", we write:

Lambda bodies are allowed to use static variables, instance variables and local variables if they are final or effectively final.  Sound familiar?


[so far so good, notice the distinction between final and effectively final]

Lambdas follow the same rules for access as local and anonymous classes!  This is not a coincidence, as behind the scenes, anonymous classes are used for lambda expressions.


I don't think the second statement is quite true, which could be considered to be an implementation detail beyond the scope of exam preparation, but it has stark implications that are definitely in scope for the test, if the mock exam questions I have been seeing are to be believed (I have gotten more than one question wrong in the past in this precise regard):

The problem with saying that the rules for access in a lambda are the same ones as local and anonymous classes is that local variables can not be re-used as parameter or local variable names in a lambda, a fact that is apparently in scope for the test and an easy way to get wrong answers, whereas a local or anonymous class defines a new scope and it is legal (tho not ideal) to re-use those names and to hide them.

I was able to quickly cook up this example that shows why I find the wording there to be dangerous.
I am not advocating variable hiding, but we see far worse all the time on the certs (well, at least the mock certs!):



yields, upon attempt at compilation:
Lambda expression's local variable local cannot redeclare another local variable defined in an enclosing scope. LambdaDemo.java /lambdavsanonymousclass/src/lambdavsanonymousclass line 27
Lambda expression's parameter local cannot redeclare another local variable defined in an enclosing scope. LambdaDemo.java /lambdavsanonymousclass/src/lambdavsanonymousclass line 26

The authors and better readers will realize this at this point, but as it is important to remember I find the sentences highlighted to be harmful in remembering this, and remembering to watch out for this.

Additionally, I don't believe the implementation of lambdas involves creation of an anonymous class behind the scenes either.
But it is the possible accidental implication on behalf of the reader that in addition to the rules about what you are allowed to access, what you are *not allowed to use* as a lambda parameter name or local to the lambda body are *also* the same, that I am worried about.
This will be most likely to bite someone who has used anonymous local classes for years and then fixates on and memorizes:

Lambdas follow the same rules for access as local and anonymous classes!  This is not a coincidence, as behind the scenes, anonymous classes are used for lambda expressions.



I have hit several questions like this already, and have now stopped getting them wrong.
Adding one sentence there about how, of course, unlike local and anonymous classes which define their own scope, lambda parameters and bodies are operating directly in the surrounding scope, and therefore must never try to re-use any variable name in scope at the point of their definition as a parameter or a local in their body, would address my concerns.
 
author & internet detective
Posts: 40685
827
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
That makes sense. I made a note to revisit this for the next edition.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic