ArrayTrailingComma

Since Checkstyle 3.2

Description

Checks that array initialization contains a trailing comma.
int[] a = new int[]
{
  1,
  2,
  3,
};
        

By default, the check demands a comma at the end if neither left nor right curly braces are on the same line as the last element of the array.

return new int[] { 0 };
return new int[] { 0
  };
return new int[] {
  0 };
        

Rationale: Putting this comma in makes it easier to change the order of the elements or add new elements on the end. Main benefit of a trailing comma is that when you add new entry to an array, no surrounding lines are changed.

{
  100000000000000000000,
  200000000000000000000, // OK
}

{
  100000000000000000000,
  200000000000000000000,
  300000000000000000000,  // Just this line added, no other changes
}
        

If closing brace is on the same line as trailing comma, this benefit is gone (as the check does not demand a certain location of curly braces the following two cases will not produce a violation):

{100000000000000000000,
 200000000000000000000,} // Trailing comma not needed, line needs to be modified anyway

{100000000000000000000,
 200000000000000000000, // Modified line
 300000000000000000000,} // Added line
        

If opening brace is on the same line as trailing comma there's also (more arguable) problem:

{100000000000000000000, // Line cannot be just duplicated to slightly modify entry
}

{100000000000000000000,
 100000000000000000001, // More work needed to duplicate
}
        

Properties

name description type default value since
alwaysDemandTrailingComma Control whether to always check for a trailing comma, even when an array is inline. boolean false 8.33

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ArrayTrailingComma"/>
  </module>
</module>
        

Which results in the following violations:

public class Example1 {
  int[] numbers = {1, 2, 3};
  boolean[] bools = {
    true,
    true,
    false  // violation 'Array should contain trailing comma.'
    };

  String[][] text = {{},{},};

  double[][] decimals = {
    {0.5, 2.3, 1.1,},
    {1.7, 1.9, 0.6},
    {0.8, 7.4, 6.5}  // violation 'Array should contain trailing comma.'
  };

  char[] chars = {'a', 'b', 'c'
  };

  String[] letters = {
    "a", "b", "c"};

  int[] a1 = new int[]{
    1,
    2
    ,
  };

  int[] a2 = new int[]{
    1,
    2
  ,};
}
        

To configure check to always validate trailing comma:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ArrayTrailingComma">
        <property name="alwaysDemandTrailingComma" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {
  int[] numbers = {1, 2, 3}; // violation 'Array should contain trailing comma.'
  boolean[] bools = {
    true,
    true,
    false // violation 'Array should contain trailing comma.'
  };

  String[][] text = {{},{},};

  double[][] decimals = {
    {0.5, 2.3, 1.1,},
    {1.7, 1.9, 0.6}, // violation 'Array should contain trailing comma.'
    {0.8, 7.4, 6.5,} // violation 'Array should contain trailing comma.'
  };

  char[] chars = {'a', 'b', 'c'  // violation 'Array should contain trailing comma.'
  };

  String[] letters = {
    "a", "b", "c"}; // violation 'Array should contain trailing comma.'

  int[] a1 = new int[]{
    1,
    2
    ,
  };

  int[] a2 = new int[]{
    1,
    2
    ,};
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker