Created
September 22, 2021 20:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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