• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Interpretation of Shadowing Object Reference variable Code from K&B

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

The interpretation of code according to me :

class Foo has three objects created:

Bar myBar = new Bar();

inside changit() method

Foo myBar = new Foo();

Foo f = new Foo();

1) If i want to access the inst var of Class Bar i have to

ref Var Foo. ref Var Bar. inst var of Class Bar

f.myBar.barnum ------------- Correct

2) In the ChangeIt () method i am passing the reference of Class Bar with ref var myBar

and then i change the value myBar.barNum = 99

That means i am changing the instance variable of Class Bar from 28 to 99 by passing the reference.------------- Correct

3) When i am creating other object of Class Foo as:

Foo myBar = new Foo();

This means the 2 Classes Bar and Foo share the same object.

Therefore , when in main method when i again say

f.myBar.barNum i get 99 and not 28. ---------------- Correct

Let me know whether i am correct.

Thank you.
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you say and write this statement

Foo myBar = new Foo();

This is not written anywhere. Actually it

Now let me explain the working of code to you.

1- There is a class Bar having an instance variable myBar.
2- There is a class Foo which has an instance variable of type Bar and it is instantiated to Bar myBar = new Bar()
3- Class Foo as a changeIt() method and a main Method.

Now the flow of program goes like this

1- When you say Foo f = new Foo(), an object of type foo is intantiated.
2- Next the code wants to access the instance variable of class Bar whcih is barNum. The way to get that instance variable in Foo class is
using its own instance variable myBar. So it would be

At this point two objects are present, one object of type Foo and that foo internally has the Bar class object.

Now this statement executes

Now here, two reference variables are pointing towards the Bar object. As the instance variable and the method local variable
as the same name which is myBar, the instance variable is shadowed by method local variable myBar. Now as both of the reference
variables are pointing towards same bar object when inside changeIt() method, this statement excutes

The state of the object changes and now the value of barNum is going to be 99 when it is printed out.

Now when the above statement executes, a new object is created and now the method local reference variable myBar
is pointing towards the new object of Bar class. Lets look ahead when we execute this part

The method local reference variable changes the value of barNum to 420 and it is printed out. Remember that, it won't
effect the value of barNum of the instance variable. After this point changeIt() completes and the object goes out of scope
as it was only local to the method.

Now the statement in main will execute

I cannot understand third point you mentioned. Because there is no other object of type Foo is created, the local reference variable
is pointed towards a new object of Bar class and that object is local to the method and Bar and Foo don't share it.

Hope it helps.

Best Regards,

dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Prithiv.

After going through yours explaination i understood that i misinterpreted the third statement.

Actually i was confused with the local reference variable mybar = new Bar();

But this belongs only to the method and is of type Class Bar

I was unware of the local reference variable concept .

Thank you so much for giving such a detail explaination.

You made my concepts strong.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic