I was wondering is there is a keyboard shortcut (in IntelliJ) for creating a new instance of an object. e.g. when I type PersonObject personObject = new PersonObject, is there a way to type this faster? Instead of writing it out, or using autocomplete.
2 Answers
IntelliJ doesn’t provide one by default, but it’s easy enough to create.
What you’re after is a “Live Template”, and you maintain those in File -> Settings -> Editor/Live Templates.
So tick the “+” sign to create a new one - let’s give it the abbreviation nw
and description “Create new instance”
Have the Template text :
$class$ $variable$ = new $class$();
The words surrounded by $ are the Template variables, so using $class$ means what you type for the first will be automatically repeated for the second.
Finally click the “Define” link to set the Applicable Context to Java.
Then you’re good to go. Simply type nw
, and you should see it come up as an auto-complete suggestion, so hit Tab to bring it in and start typing the class name.

- 4,988
- 1
- 16
- 16
-
Glad to help. Even if you decide not to use them for this specific case, Live Templates are good to know. – racraman Jan 26 '22 at 06:41
-
Is it possible to make the template case sensitive, so that it autofills instance as well? – JacobTheKnitter Feb 08 '22 at 08:35
-
It will autocomplete the class name (same as if you were typing it without the Template) - is that what you mean? – racraman Feb 08 '22 at 09:34
-
Indeed, the template autocompletes class name, but it's still necessary to manually write name of the instance, which usually has the same name as the class name. – JacobTheKnitter Feb 08 '22 at 09:39
-
1@JacobTheKnitter Ah yes, that can be done. Click the “Edit Variables” button, and edit the `variable` line (since I called it $variable$) and set `expression` to `camelCase(class)` (since I called the other variable `$class$`) - note no $ or = or anything, just that text. You can also choose to tick `Skip if defined `, which means that in use the generated name is taken as-is (you won’t tab to it). – racraman Feb 09 '22 at 01:17
The Answer by racraman looks like a good one. An alternative is to type part of the line, and let IntelliJ finish it.
Introduce local variable
You can type just the new PersonObject
part, and let IntelliJ write the variable declaration and assignment.
You type:
new PersonObject()
… then either:
- Choose
Refactor > Extract/Introduce > Variable
menu item, or - Press keyboard shortcut shown in this Answer.
IntelliJ fills in the rest. You end up with:
PersonObject personObject = new PersonObject();
See the documentation page, Extract/Introduce variable.

- 303,325
- 100
- 852
- 1,154