Are there any Java method ordering conventions?
Some conventions list all the public methods first, and then all the private ones - that means it's easy to separate the API from the implementation, even when there's no interface involved, if you see what I mean.
Another idea is to group related methods together - this makes it easier to spot seams where you could split your existing large class into several smaller, more targeted ones.
Best practice on how to organize methods?
Methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much
Robert C Martin (aka Uncle Bob)
What this means (in short), is that your methodA1, methodA2 should be placed after methodA (that uses them). Same with methodB methodB1, methodB2. I would expect to see something like that:
public class MyClass {
methodA();
methodB();
methodC();
public void methodA() {
methodA1();
methodA2();
}
private void methodA1() {
do something;
}
private void methodA2() {
do something;
}
public void methodB() {
methodB1();
methodB2();
}
public void methodB1() {
do something;
}
public void methodB2() {
do something;
}
}
It's also suggested that you put your member variables at the top (thus not making the placement decision based on the access modifiers).
You may want to check Uncle Bob's books or videos to get some really good advice on writing clean code.
Does the order of a Java class matter?
The order does not matter. But there is a common courtesy to public methods and static methods at the beginning of a class. This is just a developers choice
Eclipse Members Sort Order vs Oracle Java Code Conventions
"So I'm wondering whether to modify the order in Eclipse or disable the Checkstyle check. Leaning towards disabling the Checkstyle check so everyone who joins the project doesn't have to modify their Eclipse settings but it seems a bit wrong to be ignoring the official conventions."
I would suggest changing Eclipse to conform to the rules that YOU want to use. Preferably using Oracles if that what you want.
I usually generate my Eclipse project files using Maven (so all code formatting is created automatically for new users). I know CXF uses something similar, take a look at their POMs for inspiration.
Best practice: ordering of public/protected/private within the class definition?
In Clean Code, Robert C. Martin advises coders to always put member variables at the top of the class (constants first, then private members) and methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much. This is a more sensible way to organize code rather than by access modifier.
Convention for methods declaration in Java
Class layout: see here http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html#1852
The following table describes the parts of a class or interface declaration, in the order that they should appear
- Class/interface documentation comment (/*.../)
- class or interface statement
- Class/interface implementation comment (/.../), if necessary
- Class (static) variables
- Instance variables
- Constructors
- Methods
Related Topics
Which Part of Throwing an Exception Is Expensive
How to Deserialize a List Using Gson or Another JSON Library in Java
Jsoup Cookies for Https Scraping
Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization
How to Enable Http Response Caching in Spring Boot
How to Get Annotations of a Member Variable
How Much Memory Does a String Use in Java 8
How to Use the Jersey JSON Pojo Support
How to Serialize Object to CSV File
Does Stream.Foreach Respect the Encounter Order of Sequential Streams
JSON Consumer of Polymorphic Objects
User Authentication on a Jersey Rest Service
Jackson/Hibernate, Meta Get Methods and Serialization
How to Pass C Structs Back and Forth to Java Code in Jni