【发布时间】:2011-09-23 14:25:15
【问题描述】:
这是一个很简单的问题,但我觉得有点争议。
当我编写 Java 类时,我使用以下顺序。
class Foo {
// static fields
// instance fields
// constructors
// methods (non-static and static methods are mixed but sorted based on their functionalities)
}
我读过一篇文章说:
(来自http://code.google.com/webtoolkit/makinggwtbetter.html#codestyle)
Java 类型应具有以下成员顺序:
嵌套类型(混合内部类和静态类是可以的)
静态字段
静态初始化器
静态方法
实例字段
实例初始化器
构造函数
实例方法
如果我按照文章,上面的顺序应该是
class Foo {
// static fields
// static methods
// instance fields
// constructors
// instance methods
}
在后者的情况下,我觉得在构造函数之前有一些方法很不舒服。 哪一个是更广泛使用的约定?
【问题讨论】:
标签: java coding-style convention