What's the best way to use shortcuts and autocomplete to create a new object in Intellij IDEA?

后端 未结 3 1862
暗喜
暗喜 2020-12-05 10:54

Object + shortcutObject object = new Object();

Whether there is such shortcut?

相关标签:
3条回答
  • 2020-12-05 11:47

    new Obj, Tab to complete to new Object(), Ctrl+Alt+V (Refactor | Introduce Variable):

    Object o = new Object();
    

    Change name if needed and press Enter to confirm.

    For the more convenient solution utilizing the live templates feature please check the answer below from @MarcG.

    0 讨论(0)
  • 2020-12-05 11:49

    As of 2017, improving on @aleksander's answer, I believe the best way to create a new object as efficiently as possible in Intellij IDEA, is this (click to enlarge):

    1. Go to Settings > Live Template.
    2. Then click the + to the right of the window, to add a new Live Template.
    3. Choose the abbreviation you want (Like n or newo). Description could be new Object .
    4. Add the template text: $Clazz$ $obj$ = new $Clazz$($END$);
    5. Click que Edit Variables button, and the fill in the Clazz and obj Expression and Default Value as shown in the picture above.
    6. Don't forget to click Define next to No applicable contexts, to define Java > Statement as the context.

    Now test it: Go to the editor, type n and then the TAB key.

    0 讨论(0)
  • 2020-12-05 11:55

    You can define your own Live Template. Press Crtl+Alt+Shift+S (go to Settings), then go to tab Live Templates and write down something similar to below and you will have new shortcut (newo+tab).

    0 讨论(0)
提交回复
热议问题