Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass by reference???

 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

We say that in Java language we pass by value only, look into the snippet below:


public class PassByRef
{
int i=1;
int j=2;

public void change(PassByRef obj)
{
obj.i=obj.i*10;
obj.j=obj.j*10;
}
public static void main(String a[])
{
PassByRef obj1= new PassByRef();
obj1.change(obj1);
System.out.println("*****obj1.i is **"+obj1.i);
System.out.println("*****obj1.j is **"+obj1.j);
}
}

Here we are passing the reference of the object as arguments to the method when method is called.

Thanks & Regards,

amit
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a question here. Are you asking something, or simply making an observation?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method change() can alter the state of the object currently referred to by local variable obj1 but change() cannot alter obj1 to refer to a different object. What is being passed by value is not an object but a reference variable.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, the reference is passed by value.
 
Tony Morris
Ranch Hand
Posts: 1608
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic