Are there any Java method ordering conventions?

JavaMethodsConventions

Java Problem Overview


I've got a large-ish class (40 or so methods) that is part of a package I will be submitting as course-work. Currently, the methods are pretty jumbled up in terms of utility public/private etc. and I want to order them in a sensible way. Is there a standard way of doing this? E.g. normally fields are listed before methods, the constructor(s) are listed before other methods, and getters/setters last; what about the remaining methods?

Java Solutions


Solution 1 - Java

  1. Class (static) variables: First the public class variables, then the protected, and then the private.

  2. Instance variables: First public, then protected, and then private.

  3. Constructors

  4. Methods: These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

Source: http://www.oracle.com/technetwork/java/codeconventions-141855.html

Solution 2 - Java

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.

Solution 3 - Java

The more precise link to «Code Conventions»: «Class and Interface Declarations»

Solution 4 - Java

Not sure if there is universally accepted standard but my own preferences are;

  • constructors first
  • static methods next, if there is a main method, always before other static methods
  • non static methods next, usually in order of the significance of the method followed by any methods that it calls. This means that public methods that call other class methods appear towards the top and private methods that call no other methods usually end up towards the bottom
  • standard methods like toString, equals and hashcode next
  • getters and setters have a special place reserved right at the bottom of the class

Solution 5 - Java

40 methods in a single class is a bit much.

Would it make sense to move some of the functionality into other - suitably named - classes? Then it is much easier to make sense of.

When you have fewer, it is much easier to list them in a natural reading order. A frequent paradigm is to list things either before or after you need them , in the order you need them.

This usually means that main() goes on top or on bottom.

Solution 6 - Java

My "convention": static before instance, public before private, constructor before methods, but main method at the bottom (if present).

Solution 7 - Java

Also, eclipse offers the possibility to sort class members for you, if you for some reason mixed them up:

Open your class file, the go to "Source" in the main menu and select "Sort Members".

taken from here: https://stackoverflow.com/questions/854232/sorting-methods-in-eclipse?answertab=active#tab-top

Solution 8 - Java

Are you using Eclipse? If so I would stick with the default member sort order, because that is likely to be most familiar to whoever reads your code (although it is not my favourite sort order.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfredleyView Question on Stackoverflow
Solution 1 - JavaMichaelView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaTimofey GorshkovView Answer on Stackoverflow
Solution 4 - JavaQwerkyView Answer on Stackoverflow
Solution 5 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 6 - JavaeljensoView Answer on Stackoverflow
Solution 7 - JavaStephan RichterView Answer on Stackoverflow
Solution 8 - JavafinnwView Answer on Stackoverflow