I'm seeing some very strange behavior in a java program. The failure is not reproducible, so all I have to go on is the original logs.
The class looks something like this:
public class MyClass {
public static final String MY_CONSTANT_STRING = "This should never change";
public boolean checkEndsWithCS(String inString) {
return inString.endsWith(MY_CONSTANT_STRING);
}
public String getString() {
return "Some text '" + MY_CONSTANT_STRING + "' some more text";
}
}
In the logs, I see a case where getString returns "Some text '' some more text" and checkEndsWithCS("These are not the chars you're looking for.") returns true.
I can only conclude that MY_CONSTANT_STRING is "" in this case.
No other class extends MyClass, so it's not getting overridden in a higher level class.
I don't see any sign of an out of memory condition in the log, which would seem like the most likely cause.
It's static final, so the reference shouldn't change. Strings are immutable, so the string shouldn't change.
Questions:
- When do these execute? I set a breakpoint in eclipse, and it never gets hit.
- Is there any possible way for an immutable string in a static final reference to change?
- Is there any possible way for the string not to get assigned in the first place?
- Is there any way the memory containing the string or reference could be getting "clobbered" by some other object or process?
- Any other subtle gotchas where I'm just not thinking about this the right way?
If the collective experience here can't break this loose, then I'll just take it as a sign that my data are faulty and examine it from that angle.
MyClass.MY_CONSTANT_STRING?MyClassbut not the class which uses it, you many not see the change. This is because the compiler will in line constants such as String.getString()trigger? If yes, you can check the contents ofMY_CONSTANT_STRINGthere. If no, you're not calling that method.