Static Method Java Coding Convention

This is a very simple question, but I think it is a 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 must have the following member order:

Nested types (mixing internal and static classes in order)
Static fields
Static Initializers
Static methods
Instance Fields
Instance initializers
Constructors
Instance Methods

If I follow the article, the above order should be

 class Foo { // static fields // static methods // instance fields // constructors // instance methods } 

In the latter case, I feel uncomfortable having some methods in front of the constructors. Which one is most widely used?

+7
source share
7 answers

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

From Codes 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 in which they should appear.

  • Comment on the class / interface documentation (/*.../)
  • class or interface statement
  • Comment on the implementation of the class / interface (/.../), if necessary
  • Class variables ( static )
  • Instance variables
  • Constructors
  • Methods
+19
source

Personally, I use option 2 (static fields and methods before elements and instance constructs). For me, this makes sense when scanning a file, because from a class user I can access static materials without the need for an instance. Therefore, it is nice to see them before the designers, because I do not care about the designers when using static materials.

+4
source

For the record only, this is from a GWT-related article:

We recognize that there are many great approaches. We are just trying to choose one that at least complies with Sun Java coding conventions ...

So the style that they use

  • proposed for GWT not for general use
  • slightly different from standard conventions
  • recognized as one of many good standards.

So, I would say, if there is no reason not to stick to your current agreements, why change them?

+3
source

The Java Code conventions offer the following (which you basically already do):

  • Class (static) variables: first, variables of the open class, then protected, then the packet level (without access modifier), and then private
  • Instance variables: first open, then protected, then packet level (without access modifier), and then private
  • Constructors
  • Methods These methods should be grouped by functionality, and not by volume or availability. For example, a private class method may be between two shared instance methods. The goal is to make code easier to read and understand.
+2
source

I don’t know, but for what it is worth, I do what you do. Constructors above, methods are grouped by functionality (without regard to static) below. Static methods tend to cluster.

The exception is static factory methods, which I intend to use instead of constructors - if so, they are in front of the constructors, and ctors are private / protected.

0
source

Of course, it all depends on preference ...

Your convention is more in line with the default order in Javadoc (i.e. static and non-static methods mixed together). This is what I usually do too.

However, inner classes are often placed at the bottom of the class, as they are often “secondary” or “auxiliary” classes, and it seems strange to place them in front of the main meat of the outer class.

0
source

I put static initializers and methods in front of the constructors, so I think I follow your quote.

Why discomfort? It seems small.

0
source

All Articles