IntelliJ IDEA Logo

How to generate getters without 'get' in Java with IntelliJ IDEA

With IntelliJ IDEA, by default, Java getters are generated with a get prefix.
Personally I prefer when a getter’s name is just the field’s name (so does records in Java 14).

Fortunately we can change this easily.

IntelliJ Getter template

Let’s say we have an Account class:

public final class Account {
    
    private final String username;

    private final boolean active;
    
    public Account(String username, boolean active) {
        this.username = username;
        this.active = active;
    }
}

What we want is to generate the getters username() and isActive().

Hit Alt + Insert: alt text

And select Getter: alt text

Click on ...: alt text

By default this is the default getter template:

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

We will add a new template (click on +), which will just need a few adjustements:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))
#if ($field.boolean && $field.primitive)
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))is##
#else
  ##
#end
${name}() {
  return $field.name;
}

And we are done.