Assuming we had this class
final class Foo {
private final Set bar = new HashSet<>();
public Foo() {
bar.add(\"one\");
It depends on how the access to this object is published. While the bar
Set
is final and thus guaranteed to visible to all threads, the population of the map is not guaranteed to happen before the conclusion of constructor.
This, however would be guaranteed to be thread safe regardless of how the object was created and made available.
private final Set bar;
public Foo() {
bar = new HashSet(Arrays.asList("one", "two", "three"));
}
Testing initialization safety of final fields
https://stackoverflow.com/a/23995782/676877