Java coding convention about static method

前端 未结 7 630
情深已故
情深已故 2021-02-05 16:24

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 {

    // stati         


        
相关标签:
7条回答
  • 2021-02-05 17:08

    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.

    0 讨论(0)
  • 2021-02-05 17:10

    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.

    0 讨论(0)
  • 2021-02-05 17:12

    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?

    0 讨论(0)
  • 2021-02-05 17:13

    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.
    0 讨论(0)
  • 2021-02-05 17:19

    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
    0 讨论(0)
  • 2021-02-05 17:19

    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.

    0 讨论(0)
提交回复
热议问题