| Author |
Passing Object Reference Variables
|
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
hai friends,
im unable to understand this program.
it will be more helpful if the whole program is explained.thanks in advance
|
 |
Jason Bullers
Greenhorn
Joined: Dec 27, 2011
Posts: 28
|
|
I'm not sure I follow. What do you mean "only 10 is passed to the method"? You have created a Dimension object with width 5 and height 10 and passed it to your modify() method. In modify(), you can get the 5 back with dim.width, just like you got the 10 using dim.height.
Could you elaborate a little more on what is giving you trouble? Working with objects is a fundamental concept, so if there's something tripping you up, its worth getting it sorted out before moving further.
|
 |
Yasin Kothia
Ranch Hand
Joined: Sep 25, 2009
Posts: 39
|
|
Your thinking one value is passed because the code is only printing the 'height'.
If you want the width, use which will = 5.
if you want to modify it, you will need to edit the modify method, so for e.g.
Both values are passed not just one, only one is printed out.
|
 |
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
Yasin Kothia wrote:Your thinking one value is passed because the code is only printing the 'height'.
If you want the width, use which will = 5.
if you want to modify it, you will need to edit the modify method, so for e.g.
Both values are passed not just one, only one is printed out.
yeah i got thanks alot..i got confused because of the width..the code for width was not given in that program..
|
 |
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
hai friends
im unable to understand the meaning of this paragraph and the program
"the called method can't change the caller's
variable, although for object reference variables, the called method can change the
object the variable referred to. What's the difference between changing the variable
and changing the object? For object references, it means the called method can't
reassign the caller's original reference variable and make it refer to a different object,
or null."
void bar() {
Foo f = new Foo();
doStuff(f);
}
void doStuff(Foo g) {
g.setName("Boo");
g = new Foo();
}
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
The bit about not altering the reference but altering its contents is part of “pass-by-value”. If you could alter the reference, that would be “pass-by-reference”, which C++ supports and C mimics, but Java™ neither mimics nor supports. There is a long discussion about “pass-by-value” here. Read that and see whether it helps you
By the way: where did you find that paragraph. You should always quote sources.
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
|
|
|
the part you posted in bold is pretty self-explanatory. it can seem confusing though. read it again carefully because it is the most succinct explanation i have read yet
|
SCJP
|
 |
Kalpana Periasamy
Greenhorn
Joined: Jan 05, 2012
Posts: 11
|
|
Called method can't change the caller's variable
Ex: void bar() {
int i = 5;
foo(i);
System.out.println("Value of i in bar is" + i);
}
void foo(int i){
i++;
System.out.println("Value of i in foo is "+i);
}
Output :
Value of i in foo is 6.
Value of i in bar is 5.
for object reference variables, the called method can change the
object the variable referred to.
Ex : void bar() {
Foo f = new Foo("Foo");
doStuff(f);
System.out.println(f.getName());
}
Foo doStuff(Foo g) {
g.setName("Boo");
}
Output : Boo (its changed in doStuff but reflected in caller's method)
3) For object references, it means the called method can't
reassign the caller's original reference variable and make it refer to a different object,
or null."
Ex :
void bar() {
Foo f = new Foo();
f.setName("Bar");
doStuff(f);
}
void doStuff(Foo g) {
g.setName("Boo");
g = new Foo();
g.setName("New Foo");
}
Output : Boo (value is changed in doStuff. But it cannot be made to point to a new variable. So it wont print "New Foo".
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Kalpana Periasamy wrote: . . . for object reference variables, the called method can change the object the variable referred to. . . .
For object references, it means the called method can't reassign the caller's original reference variable and make it refer to a different object, or null."
. . .
Those two statements, which I think are quoted an earlier post, seem to contradict each other. I don’t think the quote is actually wrong, however. That was a confusing statement; where does it come from? I have already asked for the source.
Did you read the old thread I quoted earlier?
|
 |
Kalpana Periasamy
Greenhorn
Joined: Jan 05, 2012
Posts: 11
|
|
. . . for object reference variables, the called method can change the object the variable referred to. . . .
Let me rephrase this sentence for better understanding. For object reference variables, the called method can change the contents of the object(referred by the variable). This is what I understood.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
You understood it correctly, despite the confusing wording. I usually say that one can manipulate the object referred to by the reference.
|
 |
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
Kalpana Periasamy wrote: . . . for object reference variables, the called method can change the object the variable referred to. . . .
Let me rephrase this sentence for better understanding. For object reference variables, the called method can change the contents of the object(referred by the variable). This is what I understood.
thanks alot for the fantastic explaination.it was very easy to understand the concepts.thanks once again
|
 |
kiruthigha rajan
Ranch Hand
Joined: Dec 29, 2011
Posts: 69
|
|
Campbell Ritchie wrote:The bit about not altering the reference but altering its contents is part of “pass-by-value”. If you could alter the reference, that would be “pass-by-reference”, which C++ supports and C mimics, but Java™ neither mimics nor supports. There is a long discussion about “pass-by-value” here. Read that and see whether it helps you
By the way: where did you find that paragraph. You should always quote sources.
this is from SCJP 1.6
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
kiruthigha rajan, Your post was moved to a new topic.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
kiruthigha rajan wrote: . . . this is from SCJP 1.6
Please be more specific. That term “SCJP” links automatically to our FAQ, which that is not a quote from.
|
 |
 |
|
|
subject: Passing Object Reference Variables
|
|
|