Java
method ordering
coding conventions
programming practices
software development

Java method ordering conventions: how to order methods in a class

Codemia Knowledge Hub ·

Java does not enforce any method order, and the compiler accepts methods in any sequence. What teams follow are conventions: constructors first, then methods grouped by role, or ordered by visibility from public to private. Pick one, document it, and apply it consistently, because a predictable class layout makes code easier to read and maintain.

Grouping by type and role

A popular convention groups members by type or role, which gives the class a structure that mirrors what it does:

  • Constructors: first, to initialize the instance.
  • Initializer blocks: static or instance initializers, if used.
  • Public methods: the class's interface.
  • Protected methods: methods intended for subclasses.
  • Private methods: internal helpers.
java
1public class Example {
2    // Constructor
3    public Example() {
4        // Initialization code
5    }
6
7    // Public method
8    public void performAction() {
9        // Implementation
10    }
11
12    // Protected method
13    protected void protectedMethod() {
14        // Implementation
15    }
16
17    // Private method
18    private void helperMethod() {
19        // Implementation
20    }
21}

Ordering by visibility

Another common practice orders methods from most public to most restrictive:

  • Public methods first.
  • Protected methods next.
  • Package-private methods (no modifier) after that.
  • Private methods last.

This shows at a glance what the class exposes to others and what is internal.

Ordering by function

Some developers group methods by responsibility instead, so related operations sit next to each other. This can make a class easier to follow.

Alphabetical order

Some choose alphabetical order. It makes methods easy to find by name but breaks up logical groupings, so it is less often recommended for complex classes.

AspectDescription
Related GroupingGroup methods by functionality: Constructors, Initializers, Public, Protected, Private
Visibility OrderOrder by visibility: Public, Protected, Package-Private, Private
Functional GroupingGroup related functional methods together
Alphabetical OrderOrder methods alphabetically (least common)

Making a convention stick

  • Tooling: IDEs like IntelliJ IDEA and Eclipse can sort or arrange methods automatically from user-defined rules, which helps enforce consistency.
  • Team style guide: write the chosen order into your style guide so everyone follows the same standard, which reduces confusion and merge conflicts.
  • Documentation: whatever the order, comments and documentation still supply context that ordering alone cannot.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms