How to infer and complete variable type with Intellij IDEA
Asked Answered
R

3

5

This could be a simple one and there may be duplicates of this type of question, but I can't find the answer anywhere. I want to complete the type of the variable by inferring the method's return type in Java. For example,

users = userService.findAllUsers()//This method returns List<User> objects

Now I want to place cursor on users variable and initialise it inline by selecting a "shortcut" key and Intellij will complete the above statement like below.

List<User> users = userService.findAllUsers()//This method returns List<User> objects

How is this possible? Do I need to add a custom shortcut to keymap or is there one already available like in Eclipse? Finally I would like to know the shortcut for Mac OSX if there is one available.

Ripp answered 13/7, 2015 at 11:18 Comment(0)
D
8

For Windows

Use Ctrl+Alt+V to create variable (as it says here)

For Mac OS

Use ++V

Discernment answered 13/7, 2015 at 11:37 Comment(2)
Thanks, My question was how it can be done on Mac OS X. I figured out that it is <option> + <Command> + V (with the help of the post you mentioned)Ripp
N.B. sadly this does not work in for loops ie for (user : findAllUsers())Dan
A
10

I have found the IntelliJ Postfix Code Completion very useful and powerful.

In your case you could do like this:

  • Create your expression without any variable enter image description here

  • Start typing .var at the end of the expression and you will get a context menu with var as a selectable item. Press Enter and a new variable will be created for you: enter image description here

Angwantibo answered 14/7, 2015 at 7:37 Comment(0)
D
8

For Windows

Use Ctrl+Alt+V to create variable (as it says here)

For Mac OS

Use ++V

Discernment answered 13/7, 2015 at 11:37 Comment(2)
Thanks, My question was how it can be done on Mac OS X. I figured out that it is <option> + <Command> + V (with the help of the post you mentioned)Ripp
N.B. sadly this does not work in for loops ie for (user : findAllUsers())Dan
S
0

Select the variable/expression you want to check and you can either:

  • Click CTRL+SHIFT+P and a small type info will appear.
  • Click CTRL+Q and the documentation of that expression will appear if available.
  • Hold CTRL and mouse hover the variable/expression and a small type info will appear.
Soulier answered 11/4, 2018 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.