gpt4 book ai didi

静态最终变量的 Java 命名约定

转载 作者:IT老高 更新时间:2023-10-28 11:40:27 25 4
gpt4 key购买 nike

有一条规则说:

Names representing constants (final variables) must be all uppercase using underscore to separate words (taken from http://geosoft.no/development/javastyle.html)

适用于 int 或字符串等原始类型:

private static final int MAX_COUNT = 10;

但是关于非原始类型呢?在大多数情况下,我看到了以下情况:

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

或者在单例中,实例变量不是大写的。

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

最佳答案

这仍然是一个常数。见 JLS有关常量命名约定的更多信息。但实际上,这完全是一个偏好问题。


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:

  • Constant names normally have no lowercase letters, so they will not normally obscure names of packages or types, nor will they normally shadow fields, whose names typically contain at least one lowercase letter.
  • Constant names cannot obscure method names, because they are distinguished syntactically.

关于静态最终变量的 Java 命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7259687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com