Java Language Oracle Official Code Standard Class Structure

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Order of class members

Class members should be ordered as follows:

  1. Fields (in order of public, protected and private)
  2. Constructors
  3. Factory methods
  4. Other Methods (in order of public, protected and private)

Ordering fields and methods primarily by their access modifiers or identifier is not required.

Here is an example of this order:

class Example {

    private int i;

    Example(int i) {
        this.i = i;
    }

    static Example getExample(int i) {
        return new Example(i);
    }

    @Override
    public String toString() {
        return "An example [" + i + "]";
    }

}

Grouping of class members

  • Related fields should be grouped together.
  • A nested type may be declared right before its first use; otherwise it should be declared before the fields.
  • Constructors and overloaded methods should be grouped together by functionality and ordered with increasing arity. This implies that delegation among these constructs flow downward in the code.
  • Constructors should be grouped together without other members between.
  • Overloaded variants of a method should be grouped together without other members between.


Got any Java Language Question?