Is a private final String[] array threadsafe as long as its protected?

Let's say I have a static final String[] array that is only read but will not be modified

private static final String[] myArray = { "Col1", "Col2", "Col3" };

If I can guarantee no thread will write any values to this array, and only read them, is this implementation okay and threadsafe? Or am I going to have volatility issues?

Jon Skeet
people
quotationmark

Yes, it's fine so long as nothing ever writes to the array. It will be initialized during type initialization, so all threads will "see" the values.

For the sake of readability you might want to consider using an immutable collection though (such as the ones in Guava). Otherwise you may find that while nothing changes the contents of the array in the original code, a maintenance patch changes it...

people

See more on this question at Stackoverflow