关于字段的私有静态最终关键字的快速 Java 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2832126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:22:45  来源:igfitidea点击:

Quick Java question about private static final keywords for fields

javastaticprivatefinal

提问by Spencer

I'm declaring a field:

我正在声明一个字段:

private static final String filename = "filename.txt";

First, does the order of private static finalmatter? If not, is there a standard accepted sequence or convention?

首先,顺序private static final重要吗?如果没有,是否有标准的公认顺序或约定?

Second, the filenamein my application is fixed. Is this the best was to store its value?

其次,filename在我的应用程序中是固定的。难道这是最好的存储它的价值?

采纳答案by Hobo

I use Checkstylewith Eclipse, which results in a warning if the declaration is in a different order to the one you've specified, citing the Java Language Specification (JLS). For example,

我将Checkstyle与 Eclipse 一起使用,如果声明的顺序与您指定的顺序不同,则会导致警告,引用 Java 语言规范 (JLS)。例如,

private final static String filename = "filename.txt";

results in

结果是

'static' modifier out of order with the JLS suggestions.

They have this pagewhich lists the order they expect, though following the links on that page through to the JLSI can't see anything to back up their assertion of a suggested order.

他们有这个页面列出了他们期望的顺序,尽管按照该页面上的链接到JLS,我看不到任何支持他们对建议顺序的断言的内容。

Having said that, the order they suggest seems to correspond to the order in most of the code I've seen, so it seems as good a convention as any to adopt.

话虽如此,他们建议的顺序似乎与我见过的大多数代码中的顺序相对应,因此它似乎与采用的任何约定一样好。

回答by Matthew Flaschen

  1. No. But that is the sequence I usually see used.

  2. It's a reasonable choice, but some would prefer a configuration file, either Propertiesor another file format (e.g. XML). That way, you can change the filename without recompiling.

  1. 不,但这是我通常看到使用的序列。

  2. 这是一个合理的选择,但有些人更喜欢配置文件,属性或其他文件格式(例如 XML)。这样,您无需重新编译即可更改文件名。

回答by Phil

  1. The order doesn't matter, but you can always play around with it - there's only 6 possibilities to test.

  2. I'm not aware of any convention, though I put the visibility modifier first (public/private/protected) so you can eyeball it and it lines up.

  3. If it's fixed then you can do that, but I always think something is a constant only to discover later (during testing, for example) that I want to pass it in. An argument on the command line or a properties file works for that case, and is a minimum of effort to set up.

  1. 顺序无关紧要,但您可以随时使用它 - 只有 6 种可能性可供测试。

  2. 我不知道任何约定,尽管我将可见性修饰符放在首位(公共/私有/受保护),以便您可以观察它并排列。

  3. 如果它是固定的,那么你可以这样做,但我总是认为某些东西是一个常量,只有在稍后(例如在测试期间)发现我想要传入它。命令行上的参数或属性文件适用于这种情况,并且是最少的设置工作。

回答by Jesper

It's common in Java to give constants (static finalvalues) an all-uppercase name, so I would write:

在 Java 中,给常量(static final值)一个全大写的名字是很常见的,所以我会写:

private static final String FILENAME = "filename.txt";

See also Code Conventions for the Java Programming Language. (Those are Sun's code conventions that the majority of Java programmers use).

另请参阅Java 编程语言的代码约定。(这些是大多数 Java 程序员使用的 Sun 代码约定)。

回答by fishautumn

see: http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.3.1

见:http: //docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.3.1

8.3.1 Field Modifiers

8.3.1 字段修饰符

FieldModifiers:
  FieldModifier
  FieldModifiers FieldModifier

FieldModifiers:
  FieldModifier
  FieldModifiers FieldModifier

FieldModifier: one of
  Annotation public protected private
  static final transient volatile

FieldModifier:
  注释公共保护私有
  静态最终瞬态易失性之一

...

...

If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.

如果两个或多个(不同)字段改性剂出现在字段声明,这是常规的,虽然不是必需的,它们出现在与上述在生产用于FieldModifier所示相一致的顺序。

回答by frogatto

The most accepted order of these keywords is private static final. Also you can remember the order of these keywords using PSFpattern that:

这些关键字中最被接受的顺序是private static final。您还可以使用PSF模式记住这些关键字的顺序:

P=> private / public / protected
S=> static / abstract / ...
F=> final

P=> 私有 / 公共 / 受保护
S=> 静态 / 抽象 / ...
F=> 最终

回答by serv-inc

To complete the nice answer by @Hobo aboveby a current link

通过当前链接完成上面@Hobo的好回答

8.1.1. Class Modifiers

A class declaration may include class modifiers.

     ClassModifier:
         (one of) 
         Annotation public protected private 
         abstract static final strictfp

8.1.1. 类修饰符

类声明可以包含类修饰符。

     ClassModifier:
         (one of) 
         Annotation public protected private 
         abstract static final strictfp

[...]

[...]

If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier.

如果两个或多个(不同的)类修饰符出现在一个类声明中,那么通常(尽管不是必需的)它们的出现顺序与上面 ClassModifier 产生式中所示的顺序一致。