我知道干净的代码约定要求最终的变量是Java中的ALL_CAPS.但是,如果我的变量是一个数组怎么办?一定是全部上限吗?至于下列哪一项是最佳做法:
public static final String[] MY_ARRAY = {"A", "B"};
//...
String str = MY_ARRAY[0];
要么
public static final String[] myArray = {"A", "B"};
//...
String str = myArray[0];
解决方法:
我所知道的没有Java代码约定提倡所有最终变量都应该是全部大写.常量应该全部为大写,而常量则通常引用不可变的最终字段(或故意不变异).
在这种情况下,你正在使用一个数组,并且因为它公开它可能很好地被改变.出于这个原因,我不认为这是一个常数,因此与myArray一起使用.
作为参考,这里是官方Oracle Java代码约定的引用:
The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). (ANSI constants should be avoided, for ease of debugging.)
标签:java,arrays,variables,final
来源: https://codeday.me/bug/20190528/1171089.html