关于静态方法的 Java 编码约定
这是一个非常简单的问题,但我认为这是一个有点争议的问题。
当我编写 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
}
在后者的情况下,我觉得在构造函数之前有一些方法很不舒服。 哪一种约定使用得更广泛?
It is a very simple question, but I think it is a little bit controversial.
When I code Java classes I use the following order.
class Foo {
// static fields
// instance fields
// constructors
// methods (non-static and static methods are mixed but sorted based on their functionalities)
}
I read an article that says:
(From http://code.google.com/webtoolkit/makinggwtbetter.html#codestyle)
Java types should have the following member order:
Nested Types (mixing inner and static classes is okay)
Static Fields
Static Initializers
Static Methods
Instance Fields
Instance Initializers
Constructors
Instance Methods
If I follow the article, the order above should be
class Foo {
// static fields
// static methods
// instance fields
// constructors
// instance methods
}
In the case of the latter, I feel uncomfortable having some methods before constructors.
Which one is the more widely-used convention?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 代码约定 建议如下(基本上是你已经做了什么):
The Java Code Conventions suggest the following (which is basically what you already do):
仅供记录,这是您链接的 GWT 文章中的内容:
因此,他们使用的样式
所以我想说,如果没有理由不坚持当前的惯例,为什么要改变它们呢?
Just for the record, this is from the GWT article you linked:
So the style they use
So I'd say, if there's no reason not to stick with your current conventions, why change them?
我不知道,但为了它的价值,我做你做的事。构造函数在顶部,方法在下面按功能分组(不考虑静态性)。静态方法倾向于分组。
例外的是我打算让您使用的静态工厂方法而不是构造函数 - 如果是这样,它们位于构造函数之前,并且构造函数是私有/受保护的。
I don't know, but for what it's worth, I do what you do. Constructors on top, methods grouped by functionality (with no regard to staticness) below. Static methods tend to group.
The exception is static factory methods that I intend for you to use instead of constructors -- if so, they are before constructors, and the ctors are private/protected.
当然,这完全是一个偏好问题...
您的约定与 Javadoc 中的默认顺序更加一致(即静态和非静态方法混合在一起)。这也是我通常做的事情。
然而,内部类通常被放置在类的底部,因为它们通常是“次要”或“辅助”类,并且将它们放在外部类的主要内容之前似乎很奇怪。
It's all a matter of preference, of course...
Your convention is more consistent with the default ordering in Javadoc (i.e. static and non-static methods mixed together). This is what I normally do, too.
However, inner classes are often placed at the bottom of a class as they are often 'secondary' or 'helper' classes, and it seems odd to put them before the main meat of the outer class.
我将静态初始化程序和方法放在构造函数之前,所以我想我正在遵循您的引用。
为什么会出现不适?这似乎是一件小事。
I put static initializers and methods before constructors, so I guess I'm following your citation.
Why the discomfort? It seems like a small thing.
我相信Sun(现在是Oracle)的Java 编码标准得到了更广泛的使用。这也是您目前正在使用的。
来自Java TM 编程语言的代码约定:
I believe Sun's (now Oracle's) Java coding standards are more widely used. This is what you are currently using too.
From Code Conventions for the Java TM Programming Language :
我个人使用选项 2(实例元素和构造之前的静态字段和方法)。对我来说,这在扫描文件时是有意义的,因为从类的用户中,我可以访问静态内容而不需要实例。因此,很高兴在构造函数之前看到它们,因为在使用静态东西时我不关心构造函数。
Personally I use option 2 (static fields and methods prior to instance elements and constructs). To me this makes sense when scanning a file because from a user of a class, I can access the static stuff without needing an instance. Therefore it is nice to see them prior to the constructors because I don't care about constructors when using static stuff.