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

发布于 2024-12-06 07:36:14 字数 822 浏览 2 评论 0原文

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

当我编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

濫情▎り 2024-12-13 07:36:15

Java 代码约定 建议如下(基本上是你已经做了什么):

  • 类(静态)变量:首先是公共类变量,然后是受保护的,然后是包级别的(无访问修饰符),然后是私有
  • 实例变量:首先是公共的,然后是受保护的,然后是包级别的(无访问权限)修饰符),然后是私有的
  • 构造函数
  • 方法:这些方法应按功能而不是按范围或可访问性进行分组。例如,私有类方法可以位于两个公共实例方法之间。目标是使阅读和理解代码更容易。

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

  • 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.
别挽留 2024-12-13 07:36:15

仅供记录,这是您链接的 GWT 文章中的内容:

我们承认存在很多很棒的方法。我们只是想选择一个至少在某种程度上符合 Sun 的 Java 编码约定的方法...

因此,他们使用的样式

  1. 是为 GWT 提出的,而不是一般用途,
  2. 在某种程度上偏离了标准约定,
  3. 被认为是许多良好标准之一

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

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

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

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

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

养猫人 2024-12-13 07:36:15

我不知道,但为了它的价值,我做你做的事。构造函数在顶部,方法在下面按功能分组(不考虑静态性)。静态方法倾向于分组。

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

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.

一曲爱恨情仇 2024-12-13 07:36:15

当然,这完全是一个偏好问题...

您的约定与 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.

雨的味道风的声音 2024-12-13 07:36:15

我将静态初始化程序和方法放在构造函数之前,所以我想我正在遵循您的引用。

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

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.

毁梦 2024-12-13 07:36:14

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

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

3.1.3 类和接口声明

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

  1. 类/接口文档注释 ( /*.../)
  2. 接口语句
  3. 类/接口实现注释(/.../)(如有必要)
  4. 类(静态)变量
  5. 实例变量
  6. 构造函数
  7. 方法

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
无敌元气妹 2024-12-13 07:36:14

我个人使用选项 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文