import java.util.Optional; class Test { private void checkIsPresent(boolean b) { Optional test; if (b) { test = Optional.of("x"); } else { test = Optional.empty(); if(!test.isPresent()) { System.out.println("Always"); } } Optional other = test; if(test.isPresent() && other.isPresent()) { System.out.println(test.get()); } } }