I'm trying to make a Java method close()
which closes object if it is open, and which returns true if the action succeeded.
public boolean close() {
if (open) {
open = false;
return true;
} else {
return false;
}
}
I'm trying to find a way to write this down with less code. What I came up with is the following.
public boolean close() {
return (open && (open = false));
}
Eclipse doesn't give any errors when I write this down. The left hand side of the evaluation checks if open is true, and if so, the right hand side should set open to false and return true as well.
I've tested the code, and open does get set to false, but it doesn't return true. Is there a way to make this work?