IntelliJ getter/setter format (single-line versus multi-line)

Marcus Leon picture Marcus Leon · Nov 23, 2009 · Viewed 16.4k times · Source

How can you get IntelliJ to generate getter/setters accessor methods on one line like this:

public String getAbc() { return abc; }

… instead of multiple lines like this:

public String getAbc() {
   return abc;
}

Answer

Alex G picture Alex G · Mar 31, 2015

I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour.

Just use the "Generate..." option, or use Alt+Insert shortcut, and select "Getter and Setter".

In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top. Use the "..." button next to the dropdown, to edit the template.

Select "IntelliJ Default" and click the "Copy" button to create a new one named "AlwayStartWithGet", which you can edit.

Just remove the following section:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

And replace it with a simple

get##

You should be left with:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

Now you can use the custom template when generating code, by selecting it in the getter template dropdown.