# 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:

  1. Class (static) variables
  2. Instance variables
  3. Constructors
  4. 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:

  1. public
  2. protected
  3. none (package private)
  4. 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:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID RearrangeClassMembers
First seen in jSparrow version 1.1.0
Minimum Java version 1.1
Remediation cost 5 min
Links