Code templates in NetBeans

During software coding it is very common to use many times same instructions or group of instructions, even in the same file.

For example, in Java we frequently have lines like the following:

private static final String COD = "123";

The sequence “private static final String” is 27 chars long, not so short and prone to typing errors.

To help us to reduce typing effort and errors, most of the IDE supports the template concept: sequence of chars which the tool expand in a longer sequence char.

For examples in NetBeans I can type psfs and press TAB to have private static final String. Nice.

Another example is the fori template that creates a complete for loop.

The list of templates can be found on Options -> Editor -> Code Templates. It is worth to have a look at this list to identify most interesting templates.

Netbeans Code template window

The available code templates can also be modified and expanded to cover code instructions and structures we mostly use in our projects.

In my project I added the followings:

abbreviation la
final ${stype type="java.util.List" default="List" editable="false"}<${paramType default="String"}> ${var newVarName default="list"} = new ${listType type="java.util.ArrayList" editable="false"}<${paramType}>(0);

it creates a line like

final List<String> list = new ArrayList<String>(0)

The template

abbreviation ve
${coltype type="org.apache.commons.lang.Validate" default="Validate" editable="false"}.notEmpty(${cursor});

it creates following line (adding the correct import if necessary):



abbreviation vn
${coltype type="org.apache.commons.lang.Validate" default="Validate" editable="false"}.notNull(${cursor});

it creates line with Validate.notNull(…) method call.

Note: I use the Apache Commons Lang Validate functions to validate public methods arguments.

More details on the code templates syntax can be found at