关于静态方法的Java编码约定 [英] Java coding convention about static method
问题描述
这是一个非常简单的问题,但我认为它有点争议。
当我编写Java类时,我使用以下顺序。
class Foo {
//静态字段
//实例字段
//构造函数
//方法(非静态和静态方法混合,但是基于它们的功能排序)
}
我阅读了一篇文章说:
(来自 http:/ /code.google.com/webtoolkit/makinggwtbetter.html#codestyle )
Java类型应具有以下成员顺序:
嵌套类型(混合内部类和静态类是可以的)
静态字段
静态初始化程序
静态方法
实例字段
实例初始化程序
构造函数
实例方法
如果我按照文章,上面的顺序应为
class Foo {
//静态字段
//静态方法
//实例字段
//构造函数
//实例方法
}
在后者的情况下,我感到不舒服在构造函数之前有一些方法。
哪一个是更广泛使用的约定?
我相信Sun更广泛使用。这是您目前使用的。
3.1.3类和接口声明
下表描述类或接口声明的部分,按照它们出现的顺序。
- 类/接口文档注释(/*.../)
class
或interface
语句
- /.../)
- 类(
static
)变量
- 实例变量
- 构造函数
- 方法
< blockquote>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 MethodsIf 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?
解决方案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 :
3.1.3 Class and Interface Declarations
The following table describes the parts of a class or interface declaration, in the order that they should appear.
- Class/interface documentation comment ( /*.../)
class
orinterface
statement- Class/interface implementation comment ( /.../), if necessary
- Class (
static
) variables- Instance variables
- Constructors
- Methods
这篇关于关于静态方法的Java编码约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!