# 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() {

		}
	}
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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