Search code examples
javaobjectintellij-ideakeyboard-shortcutsinstantiation

Keyboard shortcut to create new instance of object in java


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.


Solution

  • 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.