How to make a boolean variable switch between true and false every time a method is invoked?
boolean, java
Solution
value ^= true;
That is value xor-equals true, which will flip it every time, and without any branching or temporary variables.
Problem
I am trying to write a method that when invoked, changes a boolean variable to true, and when invoked again, changes the same variable to false, etc. For example: call method -> boolean = true -> call method -> boolean = false -> call method -> boolean = true So basically, ``` if (a = false) { a = true; } if (a = true) { a = false; } ``` I am not sure how to accomplish this, because every time I call the method, the boolean value changes to true and then false again.