• Post Reply Bookmark Topic Watch Topic
  • New Topic

pass by reference  RSS feed

 
jiju mathew
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..,
I had a code some what like ..

public static void main(String a[]){
Car c=new Car();
c.speed=60;
changeSpeed(c);
System.our.println(c.speed); // 60
}
static void changeSpeed(Car c){
System.our.println(c.speed); // 60
c=new Car();
c.speed=80;
System.our.println(c.speed); // 80

}
i expect value of speed as 80 after changeSpeed(); but got 60 ,
please clarify..
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jiju mathew:
Hi..,
I had a code some what like ..

public static void main(String a[]){
Car c=new Car();
c.speed=60;
changeSpeed(c);
System.our.println(c.speed); // 60
}
static void changeSpeed(Car c){
System.our.println(c.speed); // 60
c=new Car();
c.speed=80;
System.our.println(c.speed); // 80

}
i expect value of speed as 80 after changeSpeed(); but got 60 ,
please clarify..


Please see in changeSpeed() Method

in this line : c=new Car(); // New instance so c object in this mehtod and c object in main method is difference !!!


If you want to get 80 , you must change follow this



 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by somkiat puisungnoen:


The code which somkiat provided still contains errors. There is no "our" method in System...

Anyway, my complete program might be helpful for you...
Instead of this...


It should be this...
 
jiju mathew
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// New instance so c object in this mehtod and c object in main method is difference !!!


new instance created inside a method is a local object of that method(),is this the reason.?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jiju mathew:
// New instance so c object in this mehtod and c object in main method is difference !!!


new instance created inside a method is a local object of that method(),is this the reason.?



I think c object in changeSpeed() method is not local variable


Example

in main method
c -- point to --> Address 123

in ChangeSpeed() method

Before call new instance :: c -- point to --> Address 123
After call new instance :: c -- point to --> Address 200 (Example)

Result :: if you access/modify data in this object, access/modify in 200 not 123 , So , in main method :: value of all property not change.






Resource
http://javadude.com/articles/passbyvalue.htm
http://www-106.ibm.com/developerworks/java/library/j-passbyval/
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is because in Java nothing as refernce is passed only the copy of the refernce is passed and while coming back from the called method the refernce copy is retained .

Thanks,
Rahul Juneja
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in main, you have a variable called c that refers to a car object.

when you pass that into your changeSpeed method, you have a SECOND variable that refers to the same object.

Then, in your changeSpeed method, you say

c = new Car();

now, the second variable points to a BRAND NEW CAR. the original c doesn't change what it's referring to, nor does it's car change.

so, you change the speed on the new car. then, when you leave that method, that reference falls out of scope, the new car becomes elligible for garbage collection.

then, you return to your main, where your original c is still referring to the original car, with it's original speed of 60.

it might make more sense if you change the method to have a different variable name...
[ August 26, 2004: Message edited by: fred rosenberger ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!