Class Body Organization

Class Body Organization

The body of a class declaration should be organized in the following order:

  1. class variable field declarations

  2. instance variable field declarations

  3. constructor declarations

  4. class method declarations

  5. instance method declarations

These three elements fields, constructors, and methods are collectively referred to as " members ."

Member Access Levels

There are four access levels for class members in Java: public, protected, default, and private, in order of decreasing accessibility. A member should be given the lowest access level that is appropriate for the member. For example, a member that is never accessed outside the class should be set to private access.

Members Comments

All public members must be preceded by a documentation comment. Protected and default access members may have a documentation comment as well, at the programmer's discretion. Private fields should not have a documentation comment. However, all fields that do not have documentation comments should have single-line comments describing them, if their function is not obvious from the name .

Class and Instance Variable Field Declarations

Class variable field declarations, if any, come first. Class variables are those fields that have the keyword "static" in their declarations. Instance variable field declarations, if any, come next . Instance variables are those that do not have the keyword "static" in their declarations. A field declaration looks like the following. Elements in square brackets [] are optional.

 [Field Modifiers] Type FieldName [= Initializer]; 

Field Modifiers are any legal combination of the following keywords, in this order:

 public         protected         private         static         final         transient         volatile 

Always put field declarations on a separate line; do not group them together on a single line:

 static private int useCount, index;     // WRONG        static private int useCount;            // RIGHT        static private int index;               // RIGHT 

A field that is never changed after initialization should be declared final. This not only serves as useful documentation to the reader, but also allows the compiler to generate more efficient code.

Constructor Declarations

Constructor declarations, if any, come after any field declarations. All of the elements of the constructor declaration up to and including the opening brace { should appear on a single line (unless it is necessary to break it up into continuation lines if it exceeds the allowable line length). Example:

 /**             * Constructs a new empty FooBar.             */            public FooBar() {                value = new char[0];            } 

If there is more than one constructor, sort them lexically by formal parameter list, with constructors having more parameters always coming after those with fewer parameters. This implies that a constructor with no arguments (if it exists) is always the first one.

Class and Instance Method Declarations

Class method declarations, if any, come next. Class methods are those that have the keyword "static" in their declarations. Instance method declarations, if any, come next. Instance methods are those which do not have the keyword "static" in their declarations. All of the elements of a method declaration up to and including the opening brace { should appear on a single line (unless it is necessary to break it up into continuation lines if it exceeds the allowable line length). A method declaration looks like the following. Elements in square brackets [] are optional.

 [Method Modifiers] Type MethodName(Parameters) [throws Exceptions] { 

Method Modifiers are any combination of the following phrases, in this order:

 public         protected         private         abstract         static         final         synchronized         native 

Exceptions is the name of an exception, or a comma-separated list of exceptions. If more than one exception is given, then they should be sorted in lexical order.

A method that will never be overridden by a subclass should be declared final. This allows the compiler to generate more efficient code. Methods that are private, or declared in a class that is final, are implicitly final; however, in these cases, the method should still be explicitly declared final for clarity.

Methods are sorted in lexical order, with one exception: if there is a finalize() method, it should be the very last method declaration in the class. This makes it easy to quickly see whether a class has a finalize() method or not. If possible, a finalize() method should call super.finalize() as the last action it performs . If the method declaration has one or more continuation lines, then a single blank line should immediately follow the opening brace.



Software Development. Building Reliable Systems
Software Development: Building Reliable Systems
ISBN: 0130812463
EAN: 2147483647
Year: 1998
Pages: 193
Authors: Marc Hamilton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net