This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in  invoking a instance variable using ref. variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in  invoking a instance variable using ref. variable" Watch "doubt in  invoking a instance variable using ref. variable" New topic

doubt in invoking a instance variable using ref. variable

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Can any one explain me below program, the value of v.i. how we rae getting?

SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
TestObj class instance t invokes first() method which in turn calls second() method. Here we craete another object of
Value val and v is a reference variable pointing to val and thereby accessing v.i results in acessing the value of
val.i which is equal to 15 and i is the local variable which will be accessed not the argument passed.
And the print in first method will give v.i=25 and local value of i=5.

Neha Verma
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169

15 0
20 5

at line 2 you are passing the reference to an object created at line 1 and the local variable of method first(), pay attention that you are calling second(v,i) and not second(v,v.i)..

now in the second() you chnage the value of i, which is local to second() from 5 to 0.but because we use pass by value it won't be reflected back in first().

at line 4 you change the state of object reffered to by variable v in second(), which now will be reflected back to first().

at line 5 you create a new object refrrefd by val and at line 6 make 'v' which is local to second() also refer to it.but it wont be reflected back to variable 'v' in first().which mean 'v' in first() will still be referring to object created at line 1.

at line 6 you print v.i which is equivalent to val.i so 15 gets printed and you also print 'i' which is the local variable of second() and you already change it's value from 5 to 0.hence 0 also gets printed.

so after first print statement you get:: 15 0

now the controls get back to first().

because you changed the state of object referred to by 'v' at line 1 from 25 to 20 at line 4(which is reflected back..) v.i at line 3 prints 20. also 'i' that you printe at line 3 is local to first() whose value is 5

so 5 gets printed so you get:: 20 5 as next o/p.

"History would be kind to me, for I intend to write it."
Charandeep Singh
Ranch Hand

Joined: May 06, 2007
Posts: 57
Hey Divya
Can any one explain me below program, the value of v.i. how we rae getting?

When you are passing object of value to method second()
then you passing the reference to the original object and i is the copy that is because it is primitive and other is object .
but in method second when you call
Value val = new Value();
v= val
then v is now pointing to another object of Value type
so what ever modifications done on v now will not be shown in the method first.

That is the reason the value of object is modified when you call v.i=2- before
Value val = new Value();
v= val
and value of i will not changes until explicitly value is returned to first and assigned to i .

I hope it clears your doubt
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI Divya,

I spent the whole morning looking at this code and was trying to understand Priyam's explanation, which is correct, but was still not getting it. I just read this article that explains it sooo well and in pictures :-) which I prefer way more. It is a must read, please find it on

I am new to Java and this question has really cleared up a major doubt I had in Java's fundamentals. Nice query you posted...thanks. :-)
[ August 02, 2007: Message edited by: Meghna Bhardwaj ]
I agree. Here's the link:
subject: doubt in invoking a instance variable using ref. variable
It's not a secret anymore!