ChatGPT解决这个技术问题 Extra ChatGPT

What is the IntelliJ shortcut to create a local variable?

In Eclipse if you have a method:

String MyObject.getValue();

When using this you can go:

MyObject.getValue(); 

If you cursor is on the line and you hit CTRL + 1 you get a context menu to 'assign a new local variable', resulting in the following:

String value = MyObject.getValue(); 

Can you do this as easily in IntelliJ? I've search the net but without success.

ProTip if you don't remember a keyboard shortcut: Wherever you are in the code, if you hit [Ctrl] + [Shift] + [A] ([cmd] + [Shift] + [A] in Mac) it will show a list of actions by name, you can type the action you want to do (e.g. Variable) and execute it
In Eclipse, it's faster to do "Ctrl-2 l" (lower-case L).

s
soshial

Yep! This is the Introduce Variable refactoring. By default, select some text, and then hit Ctrl + Alt + V (for Mac: ++V). If the expression is incomplete or invalid, IntelliJ will still make a good guess about what you meant and try to fix it for you.


Thanks. I was using CTRL+ALT+F but this didnt offer a 'local' option.
And,you can press Shift+Tab to choose variable type,IDEA will remember your choice for future.
Is there a possibility to declare the variable final via keyboard, without clicking on the popup?
I think once you specify that the variable should be final the first time, your selection is remembered in the future (in IntelliJ 14 at least).
@John Feminella:How do you make it 'final' without making usage of the mouse ? (Intellij IDEA 2017.2.1) ?
m
micha

IntelliJ 13.1 introduced Postfix completion.

With Postfix Completion you can introduce a local variable by typing:

MyObject.getValue().var

and pressing ctrl + space or enter.

It even works inside other statements. For example:

foo.someMethod(myObject.getValue().var);

O
Olivier Royo

for mac users: alt + enter at the position where you want to generate your variable


This is my go-to method. Is there a way you know of to default to a typed instance? I often have to alt + enter again and select "Add type annotation"