Hi I have a class with a mega huge constant string
public class CharsetXYZ extends Charset {
private static class Decoder extends XYZDecoder {
private static final String INDEX= "HUGE STRING CONSTANT 1234567890abcchdefghijklllmnñopqrstuvwxyz...";
...
}
}
and findbugs reports me the error:
INDEX is initialized to a string constant 7213 characters long that is duplicated in 6 other class files
HSC_HUGE_SHARED_STRING_CONSTANT: Huge string constants is duplicated across multiple class files
The two Oracle shortcuts do not seem ideal to me:
final
remove the keyword
public class CharsetXYZ extends Charset {
private static class Decoder extends XYZDecoder {
private static String INDEX= "HUGE STRING CONSTANT 1234567890abcchdefghijklllmnñopqrstuvwxyz...";
...
}
}
wrap the constant in a static initialization block.
public class CharsetXYZ extends Charset {
private final static String INDEX;
static {
INDEX = "HUGE STRING CONSTNT2"
}
}
Any other way to fix it?
There seems to be no other way to fix it.
The variant that I consider most recommendable is to initialize the constant within a block
static{...}
since in this way you do not have to remove the wordfinal
and you avoid that the value of the variable is modifiedINDEX
.I have tested with both OracleJDK 1.8.0_172 and OpenJDK 11.0.2 and in both cases the constant is duplicated in each .class file if I don't use the block
static{...}
.