Or if the string literal itself is where the typo is, perhaps it's as
Andreas suggests: Sun realized that it's better to just have the API
respond to names already documented elsewhere, rather than providing a
separate constant that has to match (and thus offering an opportunity for
it to be wrong).
Isn't the normal and best-practise thing to do here to use the same
constant where the code reads strings looking for a match as in the code
that passes that code a string?
public void accept (String s) {
if (!s.equals("foo")) throw new IllegalArgumentException();
}
public void test () {
accept("bar");
}
compiles but fails at run-time; and as you noted,
public static final String THE_STRING = "bar"
public void accept (String s) {
if (!s.equals("foo")) throw new IllegalArgumentException();
}
public void test () {
accept(THE_STRING);
}
fails at run-time because THE_STRING has gotten out of synch with the
literal in "accept". But
public static final String THE_STRING = "bar"
public void accept (String s) {
if (!s.equals(THE_STRING)) throw new IllegalArgumentException();
}
public void test () {
accept(THE_STRING);
}
succeeds, and
public static final String THE_STRING = "bar"
public void accept (String s) {
if (!s.equals(THE_STRONG)) throw new IllegalArgumentException();
}
public void test () {
accept(THE_STRUNG);
}
fails at compile time (at both mismatches).