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

前端 未结 5 1493
猫巷女王i
猫巷女王i 2020-12-03 08:25

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

相关标签:
5条回答
  • 2020-12-03 09:07

    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 .

    0 讨论(0)
  • 2020-12-03 09:09

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 09:14

    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.

    0 讨论(0)
  • 2020-12-03 09:32

    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.

    0 讨论(0)
提交回复
热议问题