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.
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.
| Aspect | Description |
| Related Grouping | Group methods by functionality: Constructors, Initializers, Public, Protected, Private |
| Visibility Order | Order by visibility: Public, Protected, Package-Private, Private |
| Functional Grouping | Group related functional methods together |
| Alphabetical Order | Order 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.
Related reading
- Are there any problems with this way of starting an infinite loop in a Spring Boot application?
- Are there best practices for Java package organization?
- Are Thread.sleep0 and Thread.yield statements equivalent?
- Are volatile variable 'reads' as fast as normal reads?
- ArithmeticException Non-terminating decimal expansion; no exact representable decimal result
- Array or List in Java. Which is faster?
- ArrayList initialization equivalent to array initialization
- ArrayList.sort vs PriorityQueue
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.