Should I use upper-case naming to declare java constant variables?

一曲冷凌霜 提交于 2021-02-20 06:00:08

问题


My question: Should names of constant Java variables (within methods) be upper-case?

I've always been under the impression that

a) if a variable is never going to change, it should be declared final (to show/enforce that it won't change) b) it should be named in upper-case

However, I've noticed in eclipse, when changing a variable (within a method) to be final/constant, and subsequently refactoring/renaming it to something like below:

final int NODE_COUNT = 3;

I get the following warning:

This name is discouraged. According to convention, names of local variables should start with a lowercase letter.

Which makes me wonder if the upper-case rule doesn't apply in this instance (i.e. final variable within a method).


回答1:


Within methods you don't have constants, you just have local variables, that can be final. So using normal camelCase starting with lowercase is perfectly suiting there.




回答2:


Class constants should also be static (making them class-level instead of instance-level), in which case Eclipse will not warn you about using Uppercase.

Method constants should have identifiers starting with a lower-case letter, though, so I agree with your conclusion.



来源:https://stackoverflow.com/questions/10978338/should-i-use-upper-case-naming-to-declare-java-constant-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!