This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

OCA SE8 Programmer Study Guide, example in page 197

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code in the following example returns "0 2 white".

Apparently "color" is an instance variable while "height" is not.

I understand that the conflict is produced due to "length" being part of the constructor's actual parameters list, while "color" isn't. This however is not addressed in the book. The authors state that there is a naming collision due to duplicate variable name, however it is not clear why "length" is duplicate while "color" isnt' although that both variables are instantiated outside of the constructor and populated inside of it (where the instance is created).

If available please refer me to some oracle's documentation that explains this so that I could clear out the confusion.

 
Ranch Hand
Posts: 312
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioannis,
As you alluded, the key here is to understand theses different variable types.  Bunny has 3 instance variables: color, height, and length.  Bunny's constructor defines 2 local variables: length and theHeight.  You likely noticed that length is declared as both an instance variable and a local variable.  Without "this." being specified, Bunny's instance variable remains at its default "0" value, while only the local variable explicitly sets itself to itself.  Although color also does not use "this." its initial null default instance reference gets explicitly set to reference "white" because there's no conflicting color declaration.  

As far as Oracle documentation,  look for anything that speaks to local variables and/or instance variables.

I hope that helps.

Charles
 
Ioannis Gkinalas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your thorough reply Charles.

I managed to find two articles from Oracle that I think helped me clear out this issue.

I feel that these tow following snippets explain the subject well for me:

“A parameter can have the same name as one of the class's fields. If this is the case, the parameter is said to shadow the field. Shadowing fields can make your code difficult to read and is conventionally used only within constructors and methods that set a particular field”
From https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

And:
“When using same names for class’s fields and constructor’s parameters, any reference within the contractor’s body refers to the parameter, not to the field. To access the field, you must use a qualified name or the “this” keyword”
From https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

If this is of any value for the writers, the part that actually helped me understand is the: “When using same names for class’s fields and constructor’s parameters, any reference within the contractor’s body refers to the parameter, not to the field”. Also using the term "Shadowing the field" is helpful for referencing purposes.
 
Charles O'Leary
Ranch Hand
Posts: 312
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy that helped in some way and thanks for providing the Oracle docs, especially highlighting why

Ioannis Gkinalas wrote:
Shadowing fields can make your code difficult to read

 
Ioannis Gkinalas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Charles and I am happy I found this information and I contribute to the forum.

BTW it would be interesting to know the internal process of fields shadowing. For example, how can two different variables/references with same name under same scope exist in the Heap? i would expect a fault due to name conflict.

Thanks, Yanes
 
    Bookmark Topic Watch Topic
  • New Topic