How can Java assignment be made to point to an object instead of making a copy?

Posted by Matthew Piziak on Stack Overflow See other posts from Stack Overflow or by Matthew Piziak
Published on 2010-05-05T19:24:43Z Indexed on 2010/05/05 19:28 UTC
Read the original article Hit count: 188

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about assignment