If you remember, I like to write my getters this way (long story here)
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private String name;
I have been doing it manually for a while but it turns out IntelliJ has a super nice feature for that: Live Templating which allows you to use parameterized and contextual templates to generate code.
Go to your Settings->Live Templating and create a new one.
Add this template:
public $TYPE$ get$UpperName$() { return $lowername$; }
public void set$UpperName$($TYPE$ $lowername$) { this.$lowername$ = $lowername$; }
private $TYPE$ $lowername$;
Then edit the variables:
- TYPE: the expression should be classNameComplete() to refer to the class name context
- lowername: the expression should be decapitalize(UpperName). Also tick skip if defined to not have to validate the computed value (this saves you one key stroke).
I’ve named my Live Template “get”. When I type “get” and TAB, I am asked to type the property type (which is suggested). The second variable asked is the capitalized property name. Then the Live Template infers the rest (decapitalize my property name to fill up lowername).
Pretty nice feature!


That’s quite useful. To make it look more natural to me,
1) I moved “private $TYPE$ $lowername$;” to the top
2) Edited template variables and changed the priority to,
I) TYPE
II) lowername
III) UpperName
3) And set the UpperName’s expression to capitalize(lowername)
And yes I forgot to add that you can save one more keystroke by check-marking “skip if defined” for UpperName (in Edit Template Variables section)
Hi Nabeel,
That makes sense to switch the variables if you put the attribute first. I like the getter first as I put my annotations on it (for various reasons) and the getter to me is really the closest representation of a “property” in Java.
Good tip on the skip if defined. I will update the post
I’m wondering if the same thing could be achieved with Eclipse.
. But the main difference is the order. I’ll have a look at the options tomorrow to see if it can be done.
People usually start by writing the field (“private String name”), then hit Alt+Shift+R+S+Enter to generate getters and setters. The getter/setter templates can be customized to look your way
However, I have a question : how do you prevent your generated code from being formatted ? I guess that you use this one line formatting only for getter/setters, not for “standard” methods. Do you use “save actions” / automatic formatting ? Don’t they mess with your getters/setters ? I may be a blind Eclipse user asking the wrong questions to the wrong problem..
Hi Pierre-Yves,
I never do automatic formatting for a few reasons:
– my style has a couple of rules that can’t be set on today’s reformatting engines
– more importantly, reformatting makes a mess out of diffs.
I find more valuable the ability to nicely diff and commit focused changes than having perfect formatting. So I don’t have your problem. In particular, domain models almost never need reformatting besides the import optimization.
I think it would be much better if IDEA supported Lombok instead, much more powerful
Thank you! This opened my eyes
My adapted version:
private $TYPE$ $lowername$;
public $TYPE$ get$UpperName$() { return $lowername$; }
/** */
public void set$UpperName$(@NotNull $TYPE$ $lowername$) { this.$lowername$ = $lowername$; }
Where:
TYPE = complete()
lowername = decapitalize(TYPE)
UpperName = capitalize(lowername) (Skip if defined)