Hi,
I am confused in the following:
In C++ we can pass a parameter to a function by reference (having declared it as a pointer or reference variable) and if we modify it inside the function, the changes are reflected to the caller when the function returns.
This is not happening in java and I am not sure I understand why.
E.g. this is a method from an object X
public boolean aMethod(int id, myClass aClass)
{
//do some logic
aClass = new MyClass();
//configure argument object aClass
return true;
}
In the calling code:
//some code processing
myClass obj = null;
if(X.aMethod(2,obj))
{
obj.methodA();//start using the object
}
I use it in C++, i.e. to return a result that notifies that the function parameter can be used, but in java this does not work.
I enter the if(X.aMethod(2,obj)) branch but the obj is null. Why is it null?
Haven't I assigned a memory address from the heap using new inside the method aMethod(int id, myClass aClass)? Am I not passing the "address" of obj in the function?
I was expecting to have the obj properly constructed and usable in the calling code. Have I misunderstood something concerning memory in java?