Example
Order of class members
Class members should be ordered as follows:
- Fields (in order of public, protected and private)
- Constructors
- Factory methods
- 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.