Gemalto is now part of the Thales Group, find out more.

You are here

Thales IoT Developer Community

Java Code Conventions

Tutorial, January 18, 2016 - 12:02pm, 2190 views

Java Code Conventions

The following points are taken from the sun hompage for mor information see Code Conventions for the Java Programming Language.

Code conventions improve the readability of the software and allows engineers to understand new code more quickly. Remember, that 80% of a software lifetime is maintenance and even for the original author it can be hard to read his code after a few month. So following these conventions can save a lot of work and time.

  • every source file should start with a comment including the author, date of last change, copywrite notices and a brief description of the class
  • files bigger than 2000 lines are mostly confusing and should be avoided. It is better to encapsulate functions in several files
  • avoid lines longer than 80 characters (some terminals can't handle such lines). When breaking lines do this after a comma or before a operator. The new line should fit to the beginning of the expression it belongs to.
            function(longExpression1, longExpression2, longExpression3,
                     longExpression4, longExpression5);
  • It is recommended to make declaration only at the beginning of a block (class, method, ...) and only one per line. (this also eases doing the comments). Exception is the declaration of a loop variable, e.g.: for(int i=0; ...
  • use only one statement per line.
  • use braces for all control structures (if, else, while, for, ...) even if a single statement follows
  • you can use following construct for ternary expressions: alpha = (aLongBooleanExpression) ? beta : gamma;
    beta and gamma must not be null/void
public class HelloWorld {

	public static void main(String[] args) {
		boolean arg = false;
		String alpha = !arg ? myMethode01() : myMethode02();
		alpha = arg ? myMethode01() : myMethode02();


	private static String myMethode01() {
		return "Hallo";

	private static String myMethode02() {
		return "World";


The upper code prints:

  • make class members public only if really necessary
  • don't use empty lines needless. Use them to divide your code into logical blocks.
  • To understand source code quickley you must understand what happems with the data and what kind of data is used. Therefore choose names that indicate the content and the use of your variables, methods or classes.
    • use whole words and avoid acronymes
    • packages only in lower case
    • classes and interfaces consists of nouns capitalized the first letter of each word, e.g.: ImageLoader
    • methods names should be verbs in mixed case, beginning with lower letter and a capitalized letter at the beginning of each new word, e.g.: calculateNewRondomNumbers()
    • variable names should short but meaningful and should indicate the use or the intention of the variable
    • constant variables consist only of capitalized letters withe a underscore dividing serveral words, e.g.: ***_CLIENTS





Profile Picture
Michał - Gemalt...