Naming Convention for java final variable which is not static

16,658

Solution 1

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:

Solution 2

You have created a local variable, which happens to be final. Therefore your naming is correct, according to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html.

Solution 3

In java final variable names are generally declared in all caps with an underscore between words

final String METHOD_NAME = "myMethod";
Share:
16,658
Jince Martin
Author by

Jince Martin

Updated on June 04, 2022

Comments

  • Jince Martin
    Jince Martin about 2 years

    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?

  • tbsalling
    tbsalling almost 7 years
    Fields are on class or instance level. Not at method level - here they are local variables.
  • Mena
    Mena almost 7 years
    @tbsalling typo, fixed - thanks.
  • Heril Muratovic
    Heril Muratovic over 6 years
    Final variables in java have to match regular expression '^[a-z][a-zA-Z0-9]*$'.
  • ihebiheb
    ihebiheb about 4 years
    I think that this answer is not correct. This is the convention for static final variables not final variables