I have the following compiling code:
final static String test = "A";
static final String test2 = "B";
And everything compiles fine. My question is, what is the difference between these two?
The order of those modifiers isn't mandatory, as stated in the Java Specification :
FieldModifiers: FieldModifier FieldModifiers FieldModifier
FieldModifier: one of Annotation public protected private static final transient volatile
But please note that the Java Specification introduces a best practice :
If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.
Which means that the most correct solution is
static final String test2 = "B";
The fact most coders respect this "custom" means that their fellow coders parse and read the code faster and with greater ease.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With