gatinueta gatinueta - 1 year ago 125
Java Question

Naming of method-local constants in checkstyle

How should java method-local constants be named?

If a constant is used only used in the context of a method, it should be declared inside the method (as a

variable). But what syntax should be used? For me, the most readable way is to use
, but checkstyle seems to disagree:

int compute(int n) {
final int FACTOR = 27;
return FACTOR * n;

will produce the checkstyle warning:

Name 'FACTOR' must match pattern '^[a-z][a-zA-Z0-9]*$'.

See also: What are your thoughts on method scoped constants?

Answer Source

Google Java Style, the currently popular style guide, says:

Local variable names are written in lowerCamelCase [...] Even when final and immutable, local variables are not considered to be constants, and should not be styled as constants.

This is also the convention assumed by Checkstyle's LocalFinalVariableName check.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download