# Rearrange Class Members
# Description
This rule rearranges the body declarations of a class based on the Oracle Code Convention for File Organization. The order of members in a class or interface should appear as follows:
- Class (static) variables
- Instance variables
- Constructors
- Methods
Furthermore, the members of the above list are also sorted according to their access modifier. The priority of the access modifiers is as follows:
- public
- protected
- none (package private)
- private
Initializers are placed between instance variables and constructors. The rest of inner type declarations like enumeration declarations, annotation declarations and inner class declarations are placed below the method declarations.
# Benefits
Adhering to code conventions such as file member orderings improves maintainability and readability.
# Tags
# Code Changes
Pre
public class RearrangeClassMembersRule {
private void sampleMethod() {
}
private static final String A_STATIC_FINAL_FIELD = "";
class SomethingCouldBeInnerType {
public SomethingCouldBeInnerType() {
}
private String foo = "";
}
private String foo = "foo-value";
private enum Days {
Mon, Tue, Wed, Thu, Fri, Sat, Sun,
}
protected String protectedFoo;
{{
var a = Days.Mon;
}}
public static void staticMethod() {
}
String defaultModifierFoo;
public String publicFoo;
public RearrangeClassMembersRule() {
}
}
Post
public class RearrangeClassMembersRule {
private static final String A_STATIC_FINAL_FIELD = "";
private String foo = "foo-value";
protected String protectedFoo;
String defaultModifierFoo;
public String publicFoo;
{{
var a = Days.Mon;
}}
public RearrangeClassMembersRule() {
}
private void sampleMethod() {
}
public static void staticMethod() {
}
private enum Days {
Mon, Tue, Wed, Thu, Fri, Sat, Sun,
}
class SomethingCouldBeInnerType {
private String foo = "";
public SomethingCouldBeInnerType() {
}
}
}
🛠️ Auto-refactor Available
You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.