4. Class layout

The layout for a class is the order in which its different parts, such as constructors, methods, instance variables and inner classes, are placed within the implementation. By being consequent in the layout of classes, it becomes easier to find the various parts of a class in the source code, since there is no doubt where one should look for e.g. a constructor.

The parts of a class should appear in the following order:

  • constants
  • class variables
  • instance variables
  • initializers
  • constructors
  • methods
  • inner classes

Note that few classes, if any, contain all of the above parts.

If there are class parts with different access levels, they should be ordered as public, protected, package private and private. This means that public methods are placed before protected methods, which in their turn appear before any package private methods and that the private methods are put last.

Methods that are logically related, e.g. get and set methods for an attribute, should be placed together.


Previous Contents Next