Sounds like you probably want a simple method like this:
public String getValueOrDefault(String value, String defaultValue) {
return isNotNullOrEmpty(value) ? value : defaultValue;
}
Then:
String result = getValueOrDefault(System.getProperty("XYZ"), "default");
At this point, you don't need temp
... you've effectively used the method parameter as a way of initializing the temporary variable.
If you really want temp
and you don't want an extra method, you can do it in one statement, but I really wouldn't:
public class Test {
public static void main(String[] args) {
String temp, result = isNotNullOrEmpty(temp = System.getProperty("XYZ")) ? temp : "default";
System.out.println("result: " + result);
System.out.println("temp: " + temp);
}
private static boolean isNotNullOrEmpty(String str) {
return str != null && !str.isEmpty();
}
}