Home > Software Development > Java Code Style and Formatter

Java Code Style and Formatter

While writing samples for my book VMware VI and vSphere SDK, I developed a Java code formatter within Eclipse. Although there are several built-in formatters like Java conventions, Eclipse, and Eclipse 2.1, I still decided to create my own partially due to the special requirements by Prentice Hall, but mostly due to my personal flavor for Java code.

After the book, I have actually used it in all my projects except the 65 chars in each line. With wide screens today, that is for sure not enough. But considering for printing, I changed it to 80 characters.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


Here are two highlights for the coding style:

  • The placement of braces. For all the built-in formatters, the starting braces are placed at the end of a line, be it a declaration or a statement. It definitely saves spaces but makes it harder to read. When you see an ending brace, you have to look for the starting brace on different column. To make it easier, I just put a starting brace on its own line and on the same column as the ending brace. While looking for the starting braces, you just look all the way up. This applies to class/method declarations, and logical statements like if/else/for/while, except very simple assignments for arrays.
  • The indentation. Typically people use Tab for indentation. But the problem is that different editor may display the tab differently. That is why Prentice Hall suggests to use spaces instead of Tab. In my code style, I use two spaces which are long enough to show the indentation but short enough not to waste much space. These days the variable names and methods are getting longer, therefore it’s important to conserve space on indentation especially with the multiple embedded loops.

The following is a sample code showing the code style from Eclipse preview:

 * A sample source file for the code formatter preview

package mypackage;

import java.util.LinkedList;

public class MyIntStack
  private final LinkedList fStack;

  public MyIntStack()
    fStack = new LinkedList();

  public int pop()
    return ((Integer) fStack.removeFirst()).intValue();

  public void push(int elem)
    fStack.addFirst(new Integer(elem));

  public boolean isEmpty()
    return fStack.isEmpty();

If you like this code style, you can download it here and import it into your Eclipse IDE. Note that it’s a zip file because I found WordPress does not allow upload XML file so I worked around it by zipping it up.

To import it, you click Window – Preferences and open a dialog box. On the left side tree, click down to Java – Code Style – Formatter, and click Import… button on the right side pane, as shown in the following screen shot. Select the file downloaded and finish importing. By default, the latest imported style profile is the active one.

To use the style in Eclipse, you can select a project, a package, or a Java file in the package explorer, then right click mouse to pick the source menu from the context menu. You can also use shortcut keys: ALT+Shift+S to get to the source context menu. From the source context menu, just click on the Format menu item. Your code will be reformatted to this Java code style. Depending on your selection in the package explorer, the scope of reformatting can be different. For example, if you select a project, all the Java source files in your project will be reformatted to the new code style.

  1. No comments yet.
  1. No trackbacks yet.