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

17,296

Solution 1

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.

Solution 2

When a variable is used as argument to a method, it's content is always copied. (Java has only call-by-value.) What's important to understand here, is that you can only refer to objects through references. So what actually happens when you pass a variable referring to an object, is that you pass the reference to the object (by value!).

Someone may tell you "primitives are passed by value" and "non primitives are passed by reference", but that is merely because a variable can never contain an object to begin with, only a reference to an object. When this someone understands this, he will agree that even variables referring to objects are passed by value.

From Is Java "pass-by-reference" or "pass-by-value"?

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.

Solution 3

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

Solution 4

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.

Share:
17,296
Matthew Piziak
Author by

Matthew Piziak

Software Engineer at Symbiont

Updated on June 12, 2022

Comments

  • Matthew Piziak
    Matthew Piziak almost 2 years

    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?