静态最终变量的Java命名约定 [英] Java naming convention for static final variables

查看:122
本文介绍了静态最终变量的Java命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一条规则:


代表常量(最终变量)的名称必须全部为大写
使用下划线单词
(摘自 http://geosoft.no/development/javastyle.html

对于int类型或字符串类型的原始类型可以正常工作:

that works fine for primitive types like int or strings:

private static final int MAX_COUNT = 10;

但是非基本类型是什么?在大多数情况下,我看到以下内容:

But what's about non primitive types? In most cases I've seen the following:

private static final Logger log = Logger.getLogger(MyClass.class);

或单例,其中实例变量不是大写。

or in singletons, where instance variable is not in upper case.

问题是什么是正确的方式声明这些类型的变量(如日志和实例)?

The question is what is the right way to declare those types of variables (like log and instance)?

推荐答案

这仍然是一个常数。有关详细信息,请参见 JLS 常量的命名约定。

That's still a constant. See the JLS for more information regarding the naming convention for constants. But in reality, it's all a matter of preference.


接口中常量的名称类型应该是,并且 final
类类型的变量可以按照惯例是一个序列或一个或多个b $ b更多的单词,首字母缩写或缩写,所有大写,组件
由下划线_字符分隔。常量名应该是
描述性的,而不是不必要的缩写。通常他们可以是任何适当的演讲部分。常量
的名称示例包括 MIN_VALUE MAX_VALUE MIN_RADIX 字符中的 MAX_RADIX

The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech. Examples of names for constants include MIN_VALUE, MAX_VALUE, MIN_RADIX, and MAX_RADIX of the class Character.

一组代表集合的替代值的常量,或者
不太频繁地,整数值中的掩码位,有时是以常用首字母缩写作为名称有效地指定的
前缀,如:

A group of constants that represent alternative values of a set, or, less frequently, masking bits in an integer value, are sometimes usefully specified with a common acronym as a name prefix, as in:

interface ProcessStates {
  int PS_RUNNING = 0;
  int PS_SUSPENDED = 1;
}

包含常量名称的模糊是罕见的:

Obscuring involving constant names is rare:


  • 常量名称通常不包含小写字母,因此它们通常不会遮盖包或类型的名称,也不会影响其名称通常至少包含一个小写字母的字段。

  • 常数名称不能掩盖方法名称,因为它们在句法上是可区分的。

这篇关于静态最终变量的Java命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆