关于字段的 private static final 关键字的快速 Java 问题
我正在声明一个字段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我将 Checkstyle 与 Eclipse 一起使用,如果声明的顺序与您的顺序不同,则会出现警告我们引用了 Java 语言规范 (JLS) 来指定。例如,
结果为
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,
results in
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.
没有。但这是我通常看到的使用顺序。
这是一个合理的选择,但有些人更喜欢配置文件,属性或其他文件格式(例如 XML)。这样,您就可以更改文件名而无需重新编译。
No. But that is the sequence I usually see used.
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.
在 Java 中,给常量(静态最终值)一个全大写的名称是很常见的,所以我会写:
另请参阅 Java 编程语言的代码约定。 (这些是大多数 Java 程序员使用的 Sun 代码约定)。
It's common in Java to give constants (
static final
values) an all-uppercase name, so I would write:See also Code Conventions for the Java Programming Language. (Those are Sun's code conventions that the majority of Java programmers use).
这些关键字最可接受的顺序是
private static final
。您还可以使用 PSF 模式记住这些关键字的顺序:The most accepted order of these keywords is
private static final
. Also you can remember the order of these keywords using PSF pattern that:请参阅: 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.
要完成上面@Hobo的好答案,由当前链接
[...]
To complete the nice answer by @Hobo above by a current link
[...]
顺序并不重要,但你可以随时尝试 - 只有 6 种可能性可供测试。
顺序并不重要,但
我不知道有任何约定,尽管我将可见性修饰符放在第一位(公共/私有/受保护),以便您可以注视它并使其对齐。
如果它是固定的,那么你可以这样做,但我总是认为某些东西是一个常量,只是为了稍后发现(例如在测试期间)我想将它传递进去。命令行或属性文件上的参数可以工作对于这种情况,设置起来是最少的工作。
The order doesn't matter, but you can always play around with it - there's only 6 possibilities to test.
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.
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.