• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Object Reference Variables  RSS feed

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends,
im unable to understand this program.


it will be more helpful if the whole program is explained.thanks in advance
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kalpana Periasamy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiruthigha rajan,
Your post was moved to a new topic.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!