1

I am currently creating an Android app and thought that I had a problem with saving data internally, etc. It turned out that the problem had nothing to do with that, but with my understanding of Java.

The problem is that when I do

myObject1 = myObject2;

and use myObject1 in myObject3 which might be a list, or whatever (in my case a Hashtable) and then change myObject2, myObject3 gets changed accordingly, as well. What is this called, and where can I learn more about it? How do I assign myObject2 to myObject1 so that myObject1 is completely "independent"?

Community
  • 1
  • 1
sk1ll3r
  • 295
  • 4
  • 15

6 Answers6

5

Variables that are Objects in Java are called references and refer to the same location in memory. If you want two objects of the same type that don't refer to the same location in memory in need to allocate memory for them on your machine by using the new keyword.

Below both variables myObject1 and myObject2 are references to an OBJECT1 object, but that don't exist at the same memory location:

OBJECT1 myObject1 = new OBJECT1();
OBJECT1 myObject2 = new OBJECT1();

If assigning an object to another is important you can look into the clone() method or use a copy constructor:

public OBJECT1(OBJECT1 toCopy)
{
    this.field1 = toCopy.field1; 
    this.field2 = toCopy.field2; 
    this.field3 = toCopy.field3; 
    this.field4 = toCopy.field4;     
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • one more question: if my OBJECT1 is a 2D array, what then? – sk1ll3r Aug 01 '12 at 13:08
  • @sk1ll3r And you want to perform a similar copying? You would then have to iterate over all of the rows and columns and copy them to the corresponding values in your new array. Here is a good post that has an example: http://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java – Hunter McMillen Aug 01 '12 at 13:13
  • Ok, that's what I used. I was just asking if there isn't a way to do it without the loop since it just make the code messy. – sk1ll3r Aug 01 '12 at 13:25
  • Not if you want a deep copy, meaning that elements in one array aren't linked to the new array by references. If you don't mind a shallow copy you can use `System.arraycopy()` : http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,int,java.lang.Object,int,int) – Hunter McMillen Aug 01 '12 at 13:27
4

Those variables are references to an object; think of each variable as the end of a string. The other end is tied to an object. If you assign a variable, you're tieing a new string onto an object.

To create a copy, you (unsurprisingly) need to create a copy. Sometimes, this is easy: there might be a copy constructor that lets you do this:

ArrayList<String> copy = new ArrayList<String>(oldArrayList);

Other times, you may be a method that makes a copy; for example, some classes implement the clone() method:

Foo copy = (Foo) otherFoo.clone();

You just have to study the API of a class to find a way to copy an object.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Important to note though that while the copy constructor for an `ArrayList` may create a new copy of `oldArrayList` (i.e. the list) it doesn't necessarily give you a new copy of the elements in `oldArrayList`. Doing `copy.get(0).someOperation()` may write back to `oldArrayList` element 0 depending on how exactly the copy constructor is defined. – Matti Lyra Aug 01 '12 at 12:52
  • Indeed, it doesn't copy the elements, just the list itself, so you can add and remove elements in one copy without affecting the number or order of elements in the other. – Ernest Friedman-Hill Aug 01 '12 at 12:55
1

That depends on what myObject is. The word you are looking for is clone if you want to have an exact copy. But not all Objects support clone, so you may have to build your own clone method that (as @Hunter) pointed out needs to allocate new memory through the new keyword.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
0

So in Java or in any OOP language

if you consider the statement

Object obj = new Object()

obj is called the handle which actually points to the location where the actual Object is stored.

so when you do obj1 = obj you are getting 2 handles which are actually pointing to the same location

so if you do obj1.setSomething() it will be reflected on the obj.getSomething() call also.

Byter
  • 1,132
  • 1
  • 7
  • 12
0

Variables contain references to objects. In other languages, references are often called pointers. So doing myObject1 = myObject2; makes the myObject1 variable reference the same object as the myObject2 variable.

If you want to make a copy of an object, the best way is to implement it yourself, for example using a copy constructor:

public MyClass(MyClass other) {
    this.foo = other.foo;
    this.bar = other.bar;
    ...
}

and thus do

myObject1 = new MyClass(myObject2);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

When you assign an object o1 to another object o2 you make the o2 pointing to o1. So whenever you change object o1 object o2 changes accordingly.

It happens the same with objects inside a list. This is because in Java when you assing an object to another you don't copy the content but it is like you "share" it.

The only way to create another object indipendent from everything is using new and then copy each attribute.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22