static class BooleanSwitch {
private static boolean switchBooleanValue;
static boolean switchIt() {
return switchBooleanValue = !switchBooleanValue;
}
}
Now, we create a BooleanSupplier using the above class and it's switchIt method.
BooleanSupplier booleanSwitch = BooleanSwitch :: switchIt;We are using java method reference to provide it's implementation. Now, I will wrap above codes in a class and call it in the main method, to get some output. And, you will see the return value from the boolean supplier changes on each call.
import java.util.function.BooleanSupplier;
import static java.lang.System.out;
public class BooleanSupplierSwitch {
public static void main (String... args) {
BooleanSupplier booleanSwitch = BooleanSwitch :: switchIt;
for(int i=0; i<5; ++i) {
out.println("Current switch value is :- " + booleanSwitch.getAsBoolean());
}
}
static class BooleanSwitch {
private static boolean switchBooleanValue;
static boolean switchIt() {
return switchBooleanValue = !switchBooleanValue;
}
}
}
Output
Current switch value is :- true Current switch value is :- false Current switch value is :- true Current switch value is :- false Current switch value is :- true