-1

I mean, we have two objects:

MyObject object1 = new MyObject();
MyObject object2 = new MyObject();

And they have difference references, for example

com.example.utente.myapp.MyObject@38540215
com.example.utente.myapp.MyObject@95713921

Is it possible to give the second reference to the first object in such a way the first object becomes the second using the references?

I can't do object1 = object2 because I'm passing object1 with Serializable so I'm creating a copy of object1 which is called object2 which has a different reference (because I'm using Serializable) so I can't interact with the objects together. These objects are activities. I'm looking for something like activity2.setReference(...); in such a way activity2 has the same reference of activity1

Original question : Android: intent.getExtras().getSerializable(Constants.codiceMainActivity) is null

Tosch
  • 17
  • 4
  • 4
    `object1 = object2` – baao Aug 09 '17 at 17:03
  • Do you really mean that the first object becomes the second object, or that both references are to the same object (the first)? Those are two different questions with different answers. – Brick Aug 09 '17 at 17:04
  • 3
    This question so far - even with the edits - is unclear. You have answers both "yes" and "no" right now because no one can understand what you're asking. You seem to have the concepts of "object" and "reference to object" confused, and your statement that you cannot do `object1 = object2` doesn't really make any sense. Can you give some context or explain what you're really trying to do? – Brick Aug 09 '17 at 17:11
  • 2
    This is probably an XY problem. Could you explain why you want to do this? If it's homework it's a terrible homework. You cannot change one object to be another; you can only make the variable point to another object. – bcsb1001 Aug 09 '17 at 17:13
  • @Pshemo updated – Tosch Aug 09 '17 at 17:19
  • @bcsb1001 updated – Tosch Aug 09 '17 at 17:20

2 Answers2

1

Yes you could , just like this

object1 = object2;
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • @Tosch Doesn't change the answer. – shmosel Aug 09 '17 at 17:08
  • I can't do that because I'm using Serializable and I want to pass an object. The first is the original and the second is a copy with a different reference. I'd like to set the first reference to the second object – Tosch Aug 09 '17 at 17:10
  • @Tosch If you were able to pass the first object, you are allowed to pass the second too. Right ? – Suresh Atta Aug 09 '17 at 17:14
  • The OP asked about changing the object, not the references to the objects. This does not answer that question. As it stands now, it's also misleading given the question that was asked. – Brick Aug 09 '17 at 17:19
  • 1
    @Brick the OP has edit his question , check the question before the edit – Ali Faris Aug 09 '17 at 17:23
  • I grant you that the constant edits make it hard to pin down anything here, but this part has remained constant across the edits: "Is it possible to give the second reference to the first object in such a way the first object becomes the second." There is no way to do something "in such a way the first object becomes the second." Only to change the *references* to point to the same thing. – Brick Aug 09 '17 at 17:26
  • @Brick how can I point the same object without object1 = object2? – Tosch Aug 09 '17 at 18:18
  • @Tosch You cannot. I also don't think that's what you really want to do either, but you have not - even after several rounds of edits - provided a problem statement that makes sense, so it's hard to tell what you really want to do. – Brick Aug 09 '17 at 18:24
  • @Brick Also I have linked the original question, I don't know what it isn't clear – Tosch Aug 09 '17 at 18:35
1

Short answer No

Is it possible to give the second reference to the first object in such a way the first object becomes the second using the references?

You can not change the values of the object that is referenced by the first reference just by assigning the reference to another variable. Instead, if you want to change the values in an object, you must change the values in the object by calling one or methods that change the values.

For example,

object1.setAllValuesFromThisInput(object2);
DwB
  • 37,124
  • 11
  • 56
  • 82