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

Sybex OCP 11 Programmer I. Chapter 2 contradiction on local variable term explaining var feature

 
Ranch Hand
Posts: 53
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Janne and Scott. I see in different places of your book method parameters are considered to be local variables and contradictory when talking about new var feature and explaining why code does not compile exactly opposite stated.

In Chapter 2. Java Building Blocks.  Examples with var it is stated that method parameters are not local variables.




In this example, a and b are method parameters. These are not local variables.


Later on in Chapter 2. Java Building Blocks. Managing Variable Scope




There are two local variables in this method. The bitesOfCheese variable is declared inside the method. The piecesOfCheese variable is a method parameter and, as discussed earlier, it is also a local variable.


Excerpt From: Boyarsky, Jeanne. “OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide

My understanding that method parameters can be considered local variables.
 
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe first code comment is correct. Please check JLS 11. Section 4.12.3. Kinds of Variables

As for the second code comment, I believe here is more technically accurate to say scope conflict or something like that. Because this code is fine -- 2 local vars with same name:


 
Oksana Cherniavskaia
Ranch Hand
Posts: 53
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see both cases already reported in erarta https://www.selikoff.net/ocp11-1/

And the right way to distinguish local variables and parameters, they are a lot alike but not exactly. Please see the comments in erarta if you are curious

52 2 In the second to last paragraph, “they are local variables that have been” should be “they are like local variables that have been” P Bakker 1/18/21



And

59 2 In the second paragraph, it says a method parameter is a local variable. It is like a local variable for garbage collection and scope, but not in terms of local variable type inference. Riffath Khan 2/23/20

 
Mikalai Zaikin
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, as I posted above JLS distinguishes them.
And if you check for example annotations predefined targets (i.e. class element where you can apply an annotation), local variables and method parameters also treated differently, so they for sure not the same.

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




There are two local variables in this method. The bitesOfCheese variable is declared inside the method. The piecesOfCheese variable is a method parameter and, as discussed earlier, it is also a local variable.

Its about scope, when you are calling the method, you must pass a value to (int piecesOfCheese), once the value is copied to the method parameter and from now, inside the method will be treaty like a local variable. Its just about scope.

PD: sorry my english is so crap

 
reply
    Bookmark Topic Watch Topic
  • New Topic