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?
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.
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.
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.
CamelCase in Java naming conventions Java follows camel-case syntax for naming the class, interface, method, and variable.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With