So I have the function
public Foo findVariable(Foo input)
that returns an object of type Foo. I want to take that object, and then modify it in findVariable in after it has been returned. I’ve read that you can’t pass by reference in Java, so is it possible to have:
public void func1() {
Foo o = new Foo();
Foo exp = findVariable(o);
Foo newExp = new Foo(somethingDifferent);
exp = newExp;
}
What I want is that the Foo object returned by findVariable is changed to the object newExp. Sorry if this is not possible, or is simple but I can’t find it from the methods I’ve used searching. Thanks!
You can mutate the
Fooreturned byfindVariableinfunc1:But if you do:
and then call:
the
foosHandedOutByMelist will not have thenewFooobject.