Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to infer and complete variable type with Intellij IDEA

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.

like image 708
Bunti Avatar asked Jul 13 '15 11:07

Bunti


1 Answers

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

like image 159
maba Avatar answered Sep 28 '22 00:09

maba