Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created September 22, 2021 20:58
Show Gist options
  • Save thomasdarimont/105c2035a5e9352c00a0efd27bfb33ac to your computer and use it in GitHub Desktop.
Save thomasdarimont/105c2035a5e9352c00a0efd27bfb33ac to your computer and use it in GitHub Desktop.
Example usage for statics in local classes for one-time computation with Java 17
public class LocalOnceExample {
public static void main(String[] args) {
foo();
foo();
}
static int foo() {
System.out.println("start foo");
class Local {
private static final int value = compute();
private static int compute() {
System.out.println("compute");
return 42;
}
}
System.out.println("end foo");
return Local.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment