I have the following code:
Example
is a class that I have no access to, and has Object original
as an instance variable.
public class Example {
private Object original;
public Object getOriginal() {
return original;
{
}
changePointer(Object param)
is a method that could be in any arbitrary static helper class (IE, Math).
public static void changePointer(Object param) {
//Change the pointer of original
}
I want changePointer(Object o)
to be able to change the pointer of original
.
The transformation must be done in changePointer(Object param)
. IE, the following isn't useful to me:
public static void exampleMethod() {
Object original = new Object();
original = changePointer(original);
}
public static Object changePointer(Object param) {
return new Object();
}
Refer to the below (crudely drawn) diagram:
As you can see, both Object 1 and Object 2 point to the same memory location.
My question: Is it possible to change the pointer of Object 1 using only the variable Object 2?
Other Information to Consider:
- I have no access to the creation of or any of the code that is part of
Example
. - I DO have access to an instance of
Example
. - I have no access to the creation of
original
inside ofExample
. - I have no access to many of the methods that make use of
original
. changePointer(Object param)
may not be necessary. If it is possible to in some other way gain access to and changeoriginal
, that would result in the same effect.
NOTE: I understand the concept of pass by reference
vs pass by value
. I know Java is pass by value
, and that's not what I'm asking. I'm asking if there is any way, knowing that Java is pass by value
, for the above to be possible.