关于字段的 private static final 关键字的快速 Java 问题

发布于 2024-09-01 11:58:06 字数 214 浏览 7 评论 0原文

我正在声明一个字段:

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

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

其次,我的应用程序中的文件名是固定的。这是存储其价值的最佳方式吗?

I'm declaring a field:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦里人 2024-09-08 11:58:06

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

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

结果为

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

They have this page,其中列出了他们期望的顺序,尽管遵循以下链接该页面一直到 JLS 我可以'没有看到任何东西来支持他们所主张的命令。

话虽如此,他们建议的顺序似乎与我见过的大多数代码中的顺序相对应,因此这似乎是一个值得采用的约定。

I use Checkstyle with 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,

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

results in

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

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

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.

淑女气质 2024-09-08 11:58:06
  1. 没有。但这是我通常看到的使用顺序。

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

  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 Properties or another file format (e.g. XML). That way, you can change the filename without recompiling.

千紇 2024-09-08 11:58:06

在 Java 中,给常量(静态最终值)一个全大写的名称是很常见的,所以我会写:

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

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

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

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).

初心 2024-09-08 11:58:06

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

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

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

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

情魔剑神 2024-09-08 11:58:06

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

8.3.1 字段修饰符

FieldModifiers:
  字段修改器
  字段修饰符 字段修饰符

字段修饰符:
之一
  注释 public protected private
  静态最终瞬态易失性

...

如果两个或多个(不同的)字段修饰符出现在字段声明中,则按惯例(尽管不是必需的)它们按顺序出现与上面显示的 FieldModifier 的产生式一致。

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

8.3.1 Field Modifiers

FieldModifiers:
  FieldModifier
  FieldModifiers FieldModifier

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

...

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.

将军与妓 2024-09-08 11:58:06

要完成上面@Hobo的好答案,由当前链接

8.1.1。类修饰符

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

 类修饰符:
         (之一) 
         注解 public protected private 
         抽象静态最终严格fp

[...]

如果一个类中出现两个或多个(不同的)类修饰符
声明,那么按照惯例,尽管不是必需的,它们
在生产中出现的顺序与上面显示的顺序一致
对于类修饰符。

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

8.1.1. Class Modifiers

A class declaration may include class modifiers.

     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.

美羊羊 2024-09-08 11:58:06
  1. 顺序并不重要,但你可以随时尝试 - 只有 6 种可能性可供测试。

    顺序并不重要,但

  2. 我不知道有任何约定,尽管我将可见性修饰符放在第一位(公共/私有/受保护),以便您可以注视它并使其对齐。

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

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文