Lets say you want a method to return both a generated object and a boolean indicating the success or failure of doing so.
In some languages, like C, you might have a function return additional objects by having reference parameters, but you cannot do this in Java since Java is "pass-by-value", so how do you return several objects in Java?
You could do
class Result{
Result(boolean result, Object value)
{
this.result = result;
this.value = value;
}
public boolean getResult()
{
return result;
}
public Object getValue()
{
result value;
}
private boolean result;
private Object value;
}
and have your function return an instance of Result
private Result myMethod()
{
boolean result = doStuff();
Object value = getValue();
return new Result(result, value)
}
I would suggest one of two things, either return null and check for it wherever you need that object, or throw an exception in the event of an error.
On success just return the object.
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