Idea logo

The IntelliJ IDEA Blog

The Capable & Ergonomic Java IDE by JetBrains


Smarter and Faster Introduce Variable in Java

Introduce variable is one of those refactorings used many times a day. So, how can we make it work faster?
Get rid of modal dialogs!

So, in order to introduce a variable from an expression press Ctrl+Alt+V.

Remember modal dialog? No more of it! Now you can edit your code right in the editor:

Oh, you prefer List as variable type? No problem, press Shift+Tab, choose List from drop down and IntelliJ IDEA will remember your choice for future.

You already pressed Ctrl+Alt+V but then realized that such variable already exists? Not a problem! Now it is possible to reassign a variable right from introduce template. Press Ctrl+Alt+V one more time and IntelliJ IDEA will collect all available variables of appropriate types and will suggest you to choose one:

You can try it yourself using a latest IntelliJ IDEA X EAP.

And let us know what you think!

Comments below can no longer be edited.

22 Responses to Smarter and Faster Introduce Variable in Java

  1. Neville Ridley-Smith says:

    November 11, 2010


  2. Denis Tulskiy says:

    November 11, 2010

    Looks great! Just one thing (maybe you’ve already fixed it) when I press Ctrl-Alt-V and want to accept the name you suggest, I mechanically press Enter and the name is deleted 🙁 Should I make myself to press Tab?

  3. Anna Kozlova says:

    November 12, 2010

    Denis, should be fixed in 98.311. BTW, please watch Thank you

  4. Alex says:

    November 12, 2010

    Hi guys! How to customize colors in the dropdown lists (e.g. autocomplete with Ctrl + Space)? Now in 10 version it’s possible?

  5. Denis Tulskiy says:

    November 13, 2010

    @Anna Kozlova: Great, thank you. BTW, it says I don’t have access rights to that issue.

  6. Jon says:

    November 16, 2010

    The ‘reassign variable’ action isn’t working here with build 98.382 on Mac OS X 10.6.5.

    I tried with exactly the same code as in the example above. Invoking with ‘introduce variable’ intention works correctly, and suggests pressing the same shortcut again to reassign the variable. But re-invoking the shortcut has no effect (it doesn’t show the ‘title’ and ‘name’ symbols as in your example).

    Should I open a bug report?

  7. Anna Kozlova says:

    November 17, 2010

    Fixed. Thank you

  8. Ara Minosian says:

    November 18, 2010

    This is most frequent refactoring that I use. Thank you.

  9. Jon says:

    November 19, 2010

    Great, thanks!

  10. Chris says:

    January 21, 2011

    I love the fact that it remembers the variable type, but it doesn’t seem to remember the removal of the “Final” designation. I rarely use “Final” variable types and have to delete it every time I introduce a variable

  11. Anna Kozlova says:

    January 21, 2011

    Chris, that was broken in 10.0.1 but is already fixed in 10.0.2 EAP. Sorry for the inconvenience.

  12. scott says:

    April 20, 2011

    I’m using 10.0.3 and still have the final come up. Is there a setting that needs changed somewhere?

  13. Anna Kozlova says:

    April 21, 2011

    You can shift-tab, type chooser will be opened; shift-tab and you’ll be able to choose not to create finals there. IDEA 10.5 provides a different UI to configure these finals though. You may see the sample how it would look like in this blog (Post about Introduce Parameter). Thank you

  14. scott says:

    April 21, 2011

    Worked like a charm! Thanks!

  15. Bijoy Thomas says:

    October 3, 2013

    It would be nice if the introduce variable defaulted to an interface type instead of the concrete class. For e.g., introducing a variable for new HashMap() should default to Map as the type of the introduced variable instead of HashMap. If there are multiple interfaces to choose from, just pick one and have the user hit Shift-Tab to pick the right type.

  16. anet says:

    October 4, 2013

    The statistic is used to choose the the type. So if you choose Map then Map would be suggested. Suggesting interface could be overcomplicated as most of users don’t really care about the type of the local variable and the additional step would bother them rather than get any profit. Thanks for the suggestion

    • Bijoy Thomas says:

      October 8, 2013

      The type of the local variable does matter. For, e.g., if the type introduced is HashMap instead of map and now I use this variable in some undefined function and then use the create method intention, the method created would use HashMap instead of Map in the type of the argument. This is not a good idea because if I change the type of the variable to TreeMap, I now have to change the method as well. I would rather code to interfaces than implementations. Just a suggestion.

      • anet says:

        October 8, 2013

        You’ll get Map after you once choose it.

        I agree that accurately chosen interfaces are better than implementations but we have to find a solution which won’t bother you in most cases. What about Collection in your example? As local variable type it is not always acceptable, you would rather choose List or Map and a chooser is very distructing.

        • Bijoy Thomas says:

          October 8, 2013

          So, the default type should be some interface and the user can then use Shift-Tab to pick the right one.

          So if I did new LinkedList(), the type of the introduced variable should default to an interface (say Collection or List) and I can use Shift-Tab to then pick which one I want.

          I can do that even today, by using Shift-Tab and choosing Map instead of HashMap as the type.

          It would be great if IntelliJ could somehow use smart intentions and know that for commonly used Collections, default to List, Set, Map as the introduced type.

          For user defined types, default to an interface that the class implements and then the user can use Shift-Tab to pick an alternate type.

  17. Bijoy Thomas says:

    October 24, 2013

    I have configured Intellij to highlight unnecessary use of the “this” qualifier. However, I don’t see an action to automatically remove those qualifiers (similar to optimize imports). Is there someway to do this?

  18. anet says:

    October 24, 2013

    There is a quick fix for the inspection which does this optimization. It could be reached on Alt|Enter in the editor (with ability to apply for the whole file in right arrow menu) or on Apply quick fixes in the inspection’s view (scope depends on the scope where you run your inspections).

  19. Bijoy Thomas says:

    October 24, 2013

    Awesome! Thank you. What a time saver!

Discover more