Java coding convention about static method

15,114

Solution 1

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

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

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

Solution 4

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.
Share:
15,114
Heejin
Author by

Heejin

Blog: epicdevs.com Twitter: @epicdevs Github: epicdevs

Updated on June 05, 2022

Comments

  • Heejin
    Heejin about 2 years

    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?

  • Heejin
    Heejin almost 13 years
    Hmm I see. I think I am using standards. Some of Google's coding styles make me confused, especially Android examples in sdk.
  • Heejin
    Heejin almost 13 years
    Yeah that's right. I also put inner classes at the bottom of a class.
  • ealeon
    ealeon over 5 years
    what about static methods vs methods?
  • Dici
    Dici about 5 years
    If doesn't say anything specifically about static methods though. I typically put them above instance variables if they are public. It's even more true for me when they are factory methods. I'll try to find a more complete description of the standard conventions.
  • Enerccio
    Enerccio over 2 years
    counterargument is that 99% of the time you use instance so having static and inner classes stuff out of the way is much better at the bottom
  • John B
    John B over 2 years
    @Enerccio I am not sure I agree with your assertion. I find that many times classes are either all-status utilities, all instance method classes or when there is a mix, the static methods tend to be the entry points (like factory methods). So I would propose that when there is a mix, you generally start with the static methods then use the instance methods once you have an instance.
  • Enerccio
    Enerccio over 2 years
    There is also using static methods for optimization