Is there any alternative to static initializers in Java?
Just a random example:
private static List<String> list;
static {
list = new ArrayList<>();
list.add("foo")
}
Doesn't it make debugging harder?
Is there any alternative to static initializers in Java?
Just a random example:
private static List<String> list;
static {
list = new ArrayList<>();
list.add("foo")
}
Doesn't it make debugging harder?
Copyright © 2021 Jogjafile Inc.
If you need a static list you will need to initialize it **somewhere*. A static initializer is a fair choice, although in this example, you can trim it down to a one liner:
Or, if this list shouldn't be modified during the program's lifetime, ever shorter:
Or as noted in the comment, in Java 9 and above: