Search code examples
javaarraysvariablesfinal

java final array lowercase or uppercase


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];

Solution

  • 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.)