Skip to content

Instantly share code, notes, and snippets.

@kaspernielsen
Created October 15, 2021 11:24
Show Gist options
  • Save kaspernielsen/4051011928b8038979efa7e2b895668c to your computer and use it in GitHub Desktop.
Save kaspernielsen/4051011928b8038979efa7e2b895668c to your computer and use it in GitHub Desktop.
boolean init on demand.
public class FooBar {
static Boolean tmpVal;
public static void main(String[] args) {
tmpVal = true; // readFromConfig
// Assuming config is initialized before method is called
foo();
}
public static void foo() {
if (Instance.val) {
// foo
} else {
// bar
}
}
private static class Instance {
static final boolean val;
static {
val = requireNonNull(FooBar.tmpVal, "tmpVal must be initialized before first test");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment