Keyboard shortcut to create new instance of object in java
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.
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.