-
Multiple return values
I saw this question asked here by someone: "So if you're a real guru, do you know how to return multiple values from a method?" or something like that.
Happens that I need just now. I have a function that should return both a boolean and a String. I don't want to write a class for this.
I suppose I can't do this:
Code:
boolean method(String out)
{
out = "Hello there!";
}
as out is a reference to an object, but the = in the function will just change out to reference a different object, not actually changing the object (impossible for String) or the reference that was originally passed to the function.
As I am writing I've just stumbled upon the java.lang.ref package, I think it's what I need.
-
No, that doesn't help me at all...
-
Why can't you just flip them?
Code:
String method(Boolean out)
{
out = new Boolean(true);
return "Hello";
}
Or pass in a Hashtable with the variables you need?
:)
-
That would still not yield the desired result, it would still only set out to reference a new object, not change the object itself or change the reference that was passed in.
Stupid language...
-
I don't think there is any way of doing this. I think you will have to create a data object that can be populated and returned by the method. I know you don't want to but I've had to do it myself.
If anyone knows a better way I'd be VERY glad to hear it.
Sorry
HD
-
What a stupid language...
-
I know that Strings destroy the current reference and reference a new object every time they're altered, but I thought that a StringBuffer worked differently, referencing the same object for the life of the variable. Maybe a StringBuffer is what you need?
-
Yes, I could probably use a StringBuffer, but I'm not really happy with this solution...
-
I could also use an array with just one element, but it's still only a workaround.