• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing parameters to a method.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is 3, and the reason is that if we pass the entire array as an object to amethod, any change to it will be reflected on return from the method call .( i donno but in the above code the return type is void.).
Is the above stmt. true in that case also??

One more query, can anybody tell in which case the change is reflected back and in which its not.


Thanks,
Chani.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All parameter passing in Java is by value. When the formal parameter is an object reference, then a copy of the reference is sent to the method. Anything you do to the copy will affect the object referred to by the actual parameter unless you point the reference somewhere else.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Change{
public void changeIt1(B temp)
{
temp.i=10;
}
public void changeIt1(B temp)
{
temp=new B();
temp.i=10;
}
public static void main(String args[])
{
B obj1,obj2;
obj1=new B();
obj2=new B();
change c=new Change()
c.changeIt1(obj1);
c.changeIt2(obj2);
System.out.println("obj1.i="+obj1.i+" obj2.i="+obj2.i);
}
}


public class B{
public int i=5;
}

The out put of running class Change is
obj1.i=10 obj2.i=5

This is because temp in changIt1 and obj1 in main refers to the same object.
But temp in changeIt2 is reassigned a new objects reference. so temp in ChangeIt2 now donot refers the same object that is refered by obj2 in main. so object refered by obj2 is not changed inside ChangeIt2.
Please let me know if I am wrong.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're understanding is right!!

Just a little change in your code and everything will sync in..



- Mel
 
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic