[英]Why can I not pass reference address as opposed to Parcel/Serial Object?
According to the StackOverflow and the internet the way to pass an object is to either serialize or parcelize (or use "static" - frowned upon). 根据StackOverflow和Internet的说法,传递对象的方法是序列化或打包(或使用“静态”-皱眉)。
I would like to know why I can not pass the address 我想知道为什么我不能通过地址
MainActivity.toString(); // MainActivity@b0ff5f80
Then while in the new class/Activity simply change the adress of the newly created object ? 然后在新类/活动中,只需更改新创建对象的地址?
MainActivity originalMainActivity = new MainActity();
originalMainActivity = getIntent.getStringExtra("originalMainActivity");
//yes I realize the above line is incompatible....Obj != String
if the object has already been created and is sitting in the heap (I think ??) Why can we not reassign address ? 如果对象已经创建并且位于堆中(我认为是?),为什么不能重新分配地址? If we can can someone please provide an example ?
如果可以,请提供示例吗? Thank You.
谢谢。
ps (after all when we pass a class object in a Constructor are we not actually passing ref address) Thx again. ps(毕竟,当我们在构造函数中传递类对象时,我们实际上并没有传递参考地址)Thx。
You seem to be thinking in terms of addresses, as if you were in C or in C++. 您似乎在考虑地址,就像您使用C或C ++。
However: 然而:
You are completely misunderstanding the purpose of toString
: in the general case, the only valid use of toString
is printing a representation of the object, for debugging purposes. 您完全误解了
toString
的目的:在一般情况下, toString
的唯一有效用法是打印对象的表示形式,以用于调试目的。 There is nothing to say that that string can be used later to regenerate, or find the object again. 没有什么可说的,以后可以使用该字符串来重新生成或再次找到该对象。
The Java language does not give any way to access memory addresses, in part because the garbage collector is generally free to rearrange memory. Java语言没有提供任何访问内存地址的方法,部分原因是垃圾回收器通常可以自由地重新排列内存。 The only way to work around that is to pin objects and operate outside of the Java language.
解决该问题的唯一方法是固定对象并在Java语言之外进行操作。
Besides, you should understand that when you get an object out of a Parcel
, you are not necessarily in the same process (even if it is your process, your process may have been background-killed before being restarted) - so your method could only work in a restricted set of cases. 此外,您应该理解,当您从
Parcel
取出一个对象时,您不一定处于同一进程中(即使它是您的进程,您的进程也可能在重新启动之前已经被后台杀死)-因此您的方法只能在少数情况下工作。
Then while in the new class\\Activity simply change the adress of the newly created object ?
然后,在新类\\活动中,只需更改新创建对象的地址?
Among other reasons, because that is not possible in Java. 除其他原因外,因为在Java中这是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.