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.

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.

This entry was posted in Software Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.
"Super vCenter" with True HTML5 Client

Bothered by the SLOW vSphere Web Client? Want to manage ALL your VMware vCenters behind a SINGLE pane of glass? Want to search/analyze/report VMware VMs, hosts, networks, datastores, events as easily as Google the Web?
Get Free Trial Working for You within 6 Minutes