非静态的 java final 变量的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46407151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming Convention for java final variable which is not static
提问by Jince Martin
I have a method in a java class.
我在 java 类中有一个方法。
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]+)*$'
重命名这个常量名以匹配正则表达式
'^[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.
我使用这个变量的目的是在 Logger 语句中使用它来跟踪我的应用程序流。
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?
这个变量不是static
变量。我已经浏览了https://softwareengineering.stackexchange.com/questions/252243/naming-convention-final-fields-not-static。但我没有得到清晰的画面。有人可以帮助我为我的最终(非静态)变量提供正确的命名约定吗?
回答by Mena
You are talking about a local variable, scoped to your method.
Local variables follow the naming convention for most Java fields, which is camelBack
.
您正在谈论一个局部变量,范围为您的方法。局部变量遵循大多数 Java 字段的命名约定,即camelBack
.
Only compile-time constants (static final
fields declared at class level) "need" to be capitalized, with words separated by an underscore.
只有编译时常量(static final
在类级别声明的字段)“需要”大写,并用下划线分隔单词。
Some doc pages:
一些文档页面:
回答by user2023608
In java final variable names are generally declared in all caps with an underscore between words
在 Java 中,最终变量名通常全部大写,单词之间用下划线表示
final String METHOD_NAME = "myMethod";
回答by tbsalling
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.
您已经创建了一个局部变量,它恰好是最终的。因此,根据http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html,您的命名是正确的。