Here's a thing that I can't tell I'm surprised it won't work, but anyway it's interesting for me to find the explanation of this case. Imagine we have an object:
SomeClass someClass = null;
And a method that will take this object as a parameter to initialize it:
public void initialize(SomeClass someClass) {
someClass = new SomeClass();
}
And then when we call:
initialize(someClass);
System.out.println("" + someClass);
It will print:
null
Thanks for your answers!
It's impossible to do in java. In C# you'd pass the parameter using the ref
or out
keyword. There are no such keywords in java. You can see this question for details: Can I pass parameters by reference in Java?
Incidentally, for that same reason you cannot write a swap function in java that would swap two integers.
As Armen mentioned, what you want to do is not possible this way. Why not use a factory method?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With