File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interpretation of Shadowing Object Reference variable Code from K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interpretation of Shadowing Object Reference variable Code from K&B" Watch "Interpretation of Shadowing Object Reference variable Code from K&B" New topic

Interpretation of Shadowing Object Reference variable Code from K&B

dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90

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.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

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,

My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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.
I agree. Here's the link:
subject: Interpretation of Shadowing Object Reference variable Code from K&B
It's not a secret anymore!