关于静态方法的Java编码约定 [英] Java coding convention about static method

查看:177
本文介绍了关于静态方法的Java编码约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,但我认为它有点争议。



当我编写Java类时,我使用以下顺序。

  class Foo {

//静态字段
//实例字段
//构造函数
//方法(非静态和静态方法混合,但是基于它们的功能排序)
}

我阅读了一篇文章说:

(来自 http:/ /code.google.com/webtoolkit/makinggwtbetter.html#codestyle



Java类型应具有以下成员顺序:



嵌套类型(混合内部类和静态类是可以的)

静态字段

静态初始化程序

静态方法

实例字段

实例初始化程序

构造函数

实例方法



如果我按照文章,上面的顺序应为

  class Foo {

//静态字段
//静态方法
//实例字段
//构造函数
//实例方法
}

在后者的情况下,我感到不舒服在构造函数之前有一些方法。
哪一个是更广泛使用的约定?

解决方案

我相信Sun更广泛使用。这是您目前使用的。



Java TM编程语言的代码约定


3.1.3类和接口声明



下表描述类或接口声明的部分,按照它们出现的顺序。


  1. 类/接口文档注释(/*.../)

  2. class interface 语句

  3. /.../)

  4. 类( static )变量

  5. 实例变量

  6. 构造函数

  7. 方法


< 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 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?

解决方案

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.

  1. Class/interface documentation comment ( /*.../)
  2. class or interface statement
  3. Class/interface implementation comment ( /.../), if necessary
  4. Class (static) variables
  5. Instance variables
  6. Constructors
  7. Methods

这篇关于关于静态方法的Java编码约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆