Search code examples
javanaming-conventionsfinal

Naming Convention for java final variable which is not static


I have a method in a java class.

public void myMethod() {
    final String methodName = "myMethod";
}

When I ran this code through an analysis in sonar, I am getting an issue saying

Rename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'

My purpose of this variable is to use it in Logger statements to track my application flow.

This variable is not a static variable. I have gone through https://softwareengineering.stackexchange.com/questions/252243/naming-convention-final-fields-not-static. But I didn't got a clear picture. Can someone help me to give proper naming convention for my final(not static) variable?


Solution

  • You are talking about a local variable, scoped to your method. Local variables follow the naming convention for most Java fields, which is camelBack.

    Only compile-time constants (static final fields declared at class level) "need" to be capitalized, with words separated by an underscore.

    Some doc pages: