Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java coding convention about static method

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?

like image 361
Heejin Avatar asked Sep 23 '11 14:09

Heejin


People also ask

What are the rules for static methods?

Characteristics of Static MethodsA static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

What is Java code conventions?

The Java code conventions are defined by Oracle in the coding conventions document. In short, these conventions ask the user to use camel case when defining classes, methods, or variables. Classes start with a capital letter and should be nouns, like CalendarDialogView .

What is the syntax for declaring a static method?

Syntax to declare the static method:Access_modifier static void methodName() { // Method body. } The name of the class can be used to invoke or access static methods.


2 Answers

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
like image 126
dogbane Avatar answered Oct 16 '22 03:10

dogbane


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.

like image 39
John B Avatar answered Oct 16 '22 03:10

John B