简体   繁体   中英

Java objects confusion

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"?

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;     
}

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.

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.

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.

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM