That is some strange requirement.
Why don't do it the right way ( or at least the java way ) ? Is like trying to get two results from a function. Like subtract( 2 , 2 ) returns 0 and "Good morning sir".
Well anyway, you can try these two options.
1st. StringBuilder Not the best thing to use, since you're not "building" an String.
public static void doStuff(){
String msg = "";
StringBuilder sb = new StringBuilder();
if ( getMessage( sb ) ) {
System.out.println( sb );
}
}
public static boolean getMessage( StringBuilder sb ) {
if ( "".equals( sb.toString() )) {
sb.delete( 0, sb.length() );
sb.append("It was empty");
return true;
} else {
return false;
}
}
2nd StringHolder ( anything holder for that matter ) Not too java style either but do the work. This would be the one used by most java developer I know.
public static void doStuff(){
String msg = "";
String [] holder = { msg };
if ( getMessage( holder ) ){
System.out.println( holder[0] );
}
}
public static boolean getMessage( String [] holder ) {
if ( "".equals( holder[0] )) {
holder[0] = "It was empty";
return true;
} else {
return false;
}
}
3rd. Option. not what you asked but what I would do. Unfortunately violates the "return boolean" condition.
public void doStuff() {
String msg = "";
String result = getMessage( msg );
if ( result != msg ) {
System.out.println( result );
}
}
public static String getMessage( String msg ) {
if ( "".equals(msg){
return "It was empty";
}
return msg
}
Choose one.
Note
Although not everyone likes this ( Jon Skeet would say "Is not universally accepted" ) , please take a look at the java coding style I'm using ( methods starting with lower case and braces in the same line ). Sometimes it is important to respect the platform you're coding in. At least that's what I do when I code in C# ( very seldom btw )