I\'m kinda waiting for a \'no\' answer on this question.
I was interested if you can save a variable at the same time when you checking it in an if-clause.
Actually I think that is a way to do this using Java Optional, here's an example:
Optional.ofNullable("text").ifPresent( $0 -> {
System.out.println($0.toUpperCase());
});
Or in your case:
if(foo != null){
Optional.ofNullable(foo.getBar()).ifPresent(bar -> {
System.out.println("Success: " + bar);
});
}