I need a non-static instance method to return multiple values. For the sake of a simple example let's say these are boolean success
and Object obj
- but in the general case there could be more and they could be unrelated to each other. Can think of a few different ways of doing this:
Solution 1
private boolean aMethod(int aParam, Object obj) { ...set obj parameter & return value... }
Solution 2
private Pair<Boolean, Object> aMethod(int aParam) { ...set return pair values... }
Solution 3
private Object obj;
...
private boolean aMethod(int aParam) { ...set obj field & return value... }
Solution 4
private class MethodReturn { // Nested class - could be a separate class instead
boolean success;
Object obj;
// ... Getters and setters omitted for brevity
}
private MethodReturn aMethod(int aParam) { ...set return object values... }
Are there any more possibilities I might have missed? And could anyone comment as to the pros and cons of each (and ideally, which might be the best to use under most circumstances)?