I understand that clean code convention requires that final variables be ALL_CAPS in Java. But what if my variable is an array? Must it be all caps? As in which one of the following is best practice:
public static final String[] MY_ARRAY = {"A", "B"};
//...
String str = MY_ARRAY[0];
Or
public static final String[] myArray = {"A", "B"};
//...
String str = myArray[0];
No Java code conventions that I'm aware of advocates that all final variables should be all caps. Constants should be all caps, and by constants you usually refer to final fields that are immutable (or deliberately never mutated).
In this case you're using an array, and since it public it may very well be mutated. For this reason, I wouldn't count this as a constant, and therefor go with myArray
.
For reference, here's a quote from the Official Oracle Java Code Conventions:
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). (ANSI constants should be avoided, for ease of debugging.)
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