java 关于静态方法的Java编码约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7530535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:21:26  来源:igfitidea点击:

Java coding convention about static method

javacoding-styleconvention

提问by Heejin

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.

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

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)

我读了一篇文章:(
来自http://code.google.com/webtoolkit/makinggwtbetter.html#codestyle

Java types should have the following member order:

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

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?

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

回答by dogbane

I believe Sun's (now Oracle's) Java coding standards are more widely used. This is what you are currently using too.

我相信 Sun 的(现在是 Oracle 的)Java 编码标准被更广泛地使用。这也是您目前正在使用的。

From Code Conventions for the Java TM Programming Language :

来自Java TM 编程语言的代码约定

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. classor interfacestatement
  3. Class/interface implementation comment ( /.../), if necessary
  4. Class (static) variables
  5. Instance variables
  6. Constructors
  7. Methods

3.1.3 类和接口声明

下表按出现的顺序描述了类或接口声明的各个部分。

  1. 类/接口文档注释(/* .../)
  2. classinterface声明
  3. 类/接口实现注释( / .../ ),如有必要
  4. 类 ( static) 变量
  5. 实例变量
  6. 构造函数
  7. 方法

回答by John B

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.

我个人使用选项 2(实例元素和构造之前的静态字段和方法)。对我来说,这在扫描文件时很有意义,因为从类的用户那里,我可以在不需要实例的情况下访问静态内容。因此很高兴在构造函数之前看到它们,因为在使用静态内容时我不关心构造函数。

回答by Thomas

Just for the record, this is from the GWT article you linked:

只是为了记录,这是来自您链接的 GWT 文章:

We acknowledge that plenty of great approaches exist out there. We're simply trying to pick one that is at least somewhat consistent with Sun's Java coding conventions...

我们承认有很多很好的方法存在。我们只是试图选择一种至少与 Sun 的 Java 编码约定相一致的...

So the style they use

所以他们使用的风格

  1. is proposed for GWT not for general usage
  2. deviates somewhat from the standard conventions
  3. is acknowledged to be one of many good standards
  1. 建议用于 GWT 不用于一般用途
  2. 有点偏离标准约定
  3. 被公认为许多良好标准之一

So I'd say, if there's no reason not to stick with your current conventions, why change them?

所以我想说,如果没有理由不坚持你目前的惯例,为什么要改变它们?

回答by michael667

The Java Code Conventionssuggest the following (which is basically what you already do):

Java的代码规范建议以下(这基本上是什么,你已经这样做):

  • Class (static) variables: First the public class variables, then the protected, then package level (no access modifier), and then the private
  • Instance variables: First public, then protected, then package level (no access modifier), and then private
  • Constructors
  • Methods: These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.
  • 类(静态)变量:首先是公共类变量,然后是受保护的,然后是包级别的(无访问修饰符),然后是私有的
  • 实例变量:首先是公共的,然后是受保护的,然后是包级别的(无访问修饰符),然后是私有的
  • 构造函数
  • 方法:这些方法应按功能分组,而不是按范围或可访问性分组。例如,私有类方法可以介于两个公共实例方法之间。目标是使阅读和理解代码更容易。

回答by Lou Franco

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.

例外是我打算让您使用的静态工厂方法而不是构造函数——如果是这样,它们在构造函数之前,并且构造函数是私有/受保护的。

回答by DNA

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.

您的约定更符合 Javadoc 中的默认顺序(即静态和非静态方法混合在一起)。这也是我平时的做法。

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 beforethe main meat of the outer class.

然而,内部类通常位于类的底部,因为它们通常是“次要”或“辅助”类,将它们放在外部类的主要内容之前似乎很奇怪。

回答by duffymo

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.

为什么会有不适感?这似乎是一件小事。