Java Programming Language Coding Conventions

Java Programming Language Coding Conventions

The following are the coding conventions of the Java programming language:

Packages – Package names should be nouns in lowercase. package shipping.objects

Classes – Class names should be nouns, in mixed case, with the first letter of each word capitalized.

class AccountBook

Interfaces – Interface names should be capitalized like class names.
interface Account

Methods – Method names should be verbs, in mixed case, with the first letter in lowercase. Within each method name, capital letters separate words. Limit the use of underscores.


Variables – All variables should be in mixed case with a lowercase first letter. Words are separated by capital letters. Limit the use of underscores, and avoid using the dollar sign ($) because this character has special meaning to inner classes.


Variables should be meaningful and indicate to the casual reader the intent of their use. Avoid single character names except for temporary throwaway variables (for example, i, j, and k, used as loop control variables).

Constants – Primitive constants should be all uppercase with the words separated by underscores. Object constants can use mixed-case letters.



Control structures – Use braces ({ }) around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement.
if ( condition ) {
} else {

Spacing – Place only a single statement on any line, and use two-space or four-space indentations to make your code readable.

The number of spaces can vary depending on what code standards you use.

Comments – Use comments to explain code segments that are not obvious. Use the // comment delimiter for normal commenting; you can comment large sections of code using the /* . . . */
delimiters. Use the /** . . . */ documenting comment to provide input to javadoc for generating HTML documentation for the code.

// A comment that takes up only one line.
/* Comments that continue past one line and take up
space on multiple lines. */
/** A comment for documentation purposes.
* @see Another class for more information