如果类
有一些public static final
字段,
那么 spring Contants 类可以帮助为这些字段提供帮助。
但是对于小写字段名称,
Contants
处理不好。
Java
规范建议
我们使用所有大写
作为public static final
字段名称,
但小写
名称仍然有效。
参见示例:
public class Test {
public static final int a = 1;
public static final int B = 2;
public static void main(String[] args) {
org.springframework.core.Constants constants = new org.springframework.core.Constants(Test.class);
System.out.println(constants.asNumber("b").intValue()); // line 1
System.out.println(constants.asNumber("a").intValue()); // line 2
}
}
第1行
将输出2
,
但第 2 行
将抛出 ConstantException:在类 Test 中找不到字段“A”
Why does not they compare them case sensitive?
最佳答案
由于约定,Java 中的编译时常量始终为UPPER_UNDERSCORE
。无一异常(exception)。
Consider class
Foo
containingpublic final static int CONSTANT1 = 66;
An instance of this class wrappingFoo.class
will return the constant value of66
from itsasNumber
method given the argument "CONSTANT1".
因此,虽然根据 Java 编程语言,采用 camelCase
或(但愿不会)lower_underscore
的名称是有效;这是否违反了公认的惯例并且不值得鼓励。
关于java - Spring Constant 将忽略并非所有大写字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234360/