views:

244

answers:

5

In a class, I have:

private Foo bar;
public Constructor(Foo bar)
{
    this.bar = bar;
}

Instead of creating a copy of bar from the object provided in the parameter, is it possible to include a pointer to bar in the constructor such that changing the original bar changes the field in this object?

Another way of putting it:

int x = 7;
int y = x;
x = 9;
System.out.print(y); //Prints 7.

It is possible to set it up so that printing y prints 9 instead of 7?

+3  A: 

Your last example works that way because int is a primitive, it is copied by value. In the first example, "this.bar" would hold a copy of the reference (sort of pointer) to bar. So if you change the original bar (internally), the change will be reflected in your class. Try it.

leonbloy
"int is a primitive, it is copied by value" may be slightly confusing. References are also copied by value.
aioobe
Yes. But the poster was thinking in term of objects. It is important to understand -to begin with- that objects (contrarily to C++) are NEVER copied by value.
leonbloy
+10  A: 

All variables are copied when passed as argument. (Java has only call-by-value.) Another thing to remember is that you can only refer to objects through references. So what actually happens when you pass an object to a function, is that you pass the reference (by value!).

Other authors may say "primitives are passed by value" and "non primitives are passed by reference", but what they mean is that, you can't pass an object to a function, you can only pass a reference of the object, (and that is passed by value just as any primitive).

From http://stackoverflow.com/questions/40480/is-java-pass-by-reference

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

From http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

In Java, there is no counter part to the C++ "reference type" for primitives.

aioobe
The first sentence is right, but might be misleading for many people who come to Java thinking that a "variable is an object" (when actually a variable is a reference to an object -or a primitive).What a beginner must first understand is that the object itself is never copied when passed as argument.
leonbloy
A: 

In order to keep the original value of member bar, you will need to implement Cloneable interface. Then before assigning a new value to the object, you will need to make a clone of it and pass the cloned value and assign new values to the cloned object. Here is a tutorial on how to do it http://www.java-tips.org/java-se-tips/java.lang/how-to-implement-cloneable-interface.html .

CoolBeans
+1  A: 

Java never copies objects. It's easiest to think of in terms of for each "new" you will have one object instance--never more.

People get REALLY CONFUSING when they discuss this in terms of pass by reference/pass by value, if you aren't amazingly familiar with what these terms mean, I suggest you ignore them and just remember that Java never copies objects.

So java works exactly the way you wanted your first example to work, and this is a core part of OO Design--the fact that once you've instantiated an object, it's the same object for everyone using it.

Dealing with primitives and references is a little different--since they aren't objects they are always copied--but the net effect is that java is just about always doing what you want it to do without extra syntax or confusing options.

Bill K
+3  A: 

To get that behavior you could modify a member of an object:

public class Number{
  int value;
  Number(int value){
    this.value = value;
  }
  public String toString() {
    return "" + value;
  }
}

You could then do:

Number x = new Number(7);
Number y = x;
x.value = 9;
System.out.println(y);//prints 9
Adam