Java Coding Standards

When writing Java code, it's recommended to follow coding standards. When writing any component (class, interface, method, variable), its name should reflect its purpose(functionality). The advantage of using appropriate names, increases the readability of the code and also increases the maintainability of the code.

class A {
    public int m1(int x, int y) {
        return x + y;
    }
}
package tld.domain.service;

public class Calculator {
    public static add(int number1, int number2) {
        return number1 + number2;
    }
}

The examples above show the difference that good coding standards can make between two classes that have the same functionality.

Coding standards for classes

Usually class names are nouns. Class names should begin with an uppercase character, and if it contains multiple words, it should be "Upper Camel Case", i.e. every subsequent word begins with an upper case character e.g (String, StringBuffer, EmptyResultDataAccessException, etc).

Coding standards for interfaces

Usually class names are adjectives. Like classes interface names should begin with an uppercase character, and be "Upper Camel Case", i.e. every subsequent word begins with an uppercase character, e.g (Runnable Serializable, RandomAccess).

Coding standards for methods

Usually method names are verbs (print(), sleep(), start(), etc) or verb-noun combination (getName(), setSalary(), etc...). Method names begin with a lower case character and every subsequent word begins with an uppercase character. Hence, method names are "Lower Camel Case".

Coding standards for variables

Usually variable names are nouns (name, age, mobileNumber). Variable names begin with a lower case character and every subsequent word begins with an uppercase character. Hence, variable names are "Lower Camel Case".

Coding standards for constants

Usually constants are nouns (MAX_VALUE, MAX_PRIORITY, NORM_PRIORITY). Constants should only contain upper case characters; if it has multiple words, these words should be separated by underscores. Constants are typically declared with public , static and final modifiers.

Coding standards for Java Beans

A Java Bean is a simple Java class with private variables and public getter and setter methods.

package tld.domain.service;

import java.lang.*;
import java.util.*;
import java.io.*;

public class StudentBean { // Ending the class name with "Bean" is not an official convention from Sun Microsystems

    private String name;
    private boolean empty;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

    public boolean isEmpty() {
        return empty;
    }
}

Coding standards for "setter" methods

The "setter" method should always be public. The return type should always be void. The method name should be prefixed with "set". The setter method should take some argument, it shouldn't be a "no-args" method.

Coding standards for "getter" methods

The "getter" method should always be public. The return type should not be void. The method name should be prefixed with "get". Setter method shouldn't take any arguments (i.e. should be a "no-args" method).

For boolean variables the "getter" method can be prefixed with either "get" or "is" e.g. "getEmpty", "isEmpty". Both of these are valid, however, the recommended prefix for boolean getter is "is"; "is" prefix is more meaningful and also reads better.

Coding standards for listeners

A listener listens to events, then performs appropriate action when the event occurs e.g. ActionListener, MouseListener, etc.

Registering listeners

Image illustrating the coding standards for registering a listener.
Image illustrating the coding standards for registering a listener.
package tld.domain.service;

import java.lang.*;
import java.util.*;
import java.io.*;

public class Test {
    public void addMyActionListener(myActionListener l) {} // VALID
    public void registerMyActionListener(myActionListener l) {} // INVALID Java Coding Standard: method name does not is not prefixed with "add"
    public void addMyActionListener(ActionListener l) {} // INVALID Java Coding Standard: argument name is not Java standard, argument name should be the same as method name, just without the "add"
}

To register a listener, the method name should be prefixed with "add".

Un-registering listeners

Image illustrating the coding standards for un-registering a listener.
Image illustrating the coding standards for un-registering a listener.
package tld.domain.service;

import java.lang.*;
import java.util.*;
import java.io.*;

public class Test {
    public void removeMyActionListener(myActionListener l) {} // VALID
    public void unregisterMyActionListener(myActionListener l) {} // INVALID Java Coding Standard: method name does not is not prefixed with "remove"
    public void deleteMyActionListener(myActionListener l) {} // INVALID Java Coding Standard: method name does not is not prefixed with "remove"
    public void removeMyActionListener(ActionListener l) {} // INVALID Java Coding Standard: argument name is not Java standard, argument name should be the same as method name, just without the "remove"
}

To un-register a listener, the method name should be prefixed with "remove".