Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Convention for java final variable which is not static [duplicate]

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?

like image 655
Jince Martin Avatar asked Sep 25 '17 14:09

Jince Martin


People also ask

What are the naming conventions for a final variable?

2.4 Names representing constants (final variables) must be all uppercase using underscore to separate words. Common practice in the Java development community and also the naming convention used by Oracle for the Java core packages. In general, the use of such constants should be minimized.

How do you name a static variable in Java?

Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Is final variable static in Java?

The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.

What are the three 3 Java naming convention?

CamelCase in Java naming conventions Java follows camel-case syntax for naming the class, interface, method, and variable.


1 Answers

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:

  • here, and better,
  • here
like image 150
Mena Avatar answered Nov 15 '22 06:11

Mena