How to infer and fill the type of a variable using Intellij IDEA
It may be simple, 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 a variable by inferring the return type of a method in Java. For example,
users = userService.findAllUsers()//This method returns List<User> objects
Now I want to place the cursor on a custom variable and initialize it inline by selecting "shortcut" and Intellij will execute the above statement as shown 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 the layout or is there one already available, for example in Eclipse? Finally, I would like to know the shortcut for Mac OSX if there is one available.
source to share
I found IntelliJ Postfix Code Completion to be very useful and powerful.
In your case, you can do the following:
-
Create your expression without any variable
-
Start typing
.var
at the end of the expression and you will get a context menu withvar
a selectable item. Click Enterand a new variable will be created for you:
source to share
Select the variable / expression you want to test and you can:
- Press CTRL+ SHIFT+ Pand a small type of information will appear.
- Click CTRL+ Qand the documentation for that expression will appear if available.
- Hold CTRLand hover your mouse over the variable / expression and a little type information will appear.
source to share