Effective java says:
// Potential security hole!
static public final Thing[] VALUES = { ... };
Can somebody tell me what is t
I would also add what Joshua Bloch proposed in Effective Java 3rd edition. Of course we can easily change the value of the array if it is declared as:
public static final String[] VALUES = { "a", "b" };
a.VALUES[0] = "changed value on index 0";
System.out.println(String.format("Result: %s", a.VALUES[0]));
and we get Result: changed value on index 0
Joshua Bloch proposed to return copy of array:
private static final String[] VALUES = { "a", "b" };
public static final String[] values()
{
return VALUES.clone();
}
so now when we try:
a.values()[0] = "changed value on index 0";
System.out.println(String.format("Result: %s", a.values()[0]));
we get Result: a
and that's what we wanted to achieve - the VALUES
are immutable.
There is also nothing bad in declaring public static final
a primitives values, Strings or other immutable objects like public static final int ERROR_CODE = 59;