When should we use an uninitialized static final variable? I know that an uninitialized static final variable can only be assigned values in the static initializer block, but I can't think of any real use for this.
It's used when initializing the variable can't be done in a single line. For example:
private static final Map<String, String> CACHE;
static {
Map<String, String> cache = new HashMap<String, String>();
cache.put("foo", "bar");
cache.put("zim", "bam");
// lots of other entries
CACHE = Collections.unmodifiableMap(cache);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With