Java : Auto import static methods in IDEs
In Java 1.5 the static method import concept was introduced.Before that whenever we are using static method we used to invoke static method on class name like Integer.parseInt(“1”). But by using static method import we can directly call parseInt(“1”) method in our code. For this to work we need to import like below.
import static java.lang.Integer.parseInt;Code language: Java (java)
In general IDEs provide content assistance feature to auto import the classes and methods. But auto import feature is not enabled by default in some IDEs and auto import feature is may not be enabled for the classes you are using in your project.
In this blog post I will show ,how to enable/achieve the auto import feature in Eclipse and Intellij Idea for static methods.
Enabling static auto import in Eclipse
One of the frequently used static methods are from “Collectors” class as we mostly deal with collections processing in daily work.
First we will see , what Eclipse content assistant ( ctrl + space) show, when we are looking for static auto import of “toList()” method from “Collectors” class.
IDE will not show any static methods as a suggestion.
Below I will show you step-by-step process to enable auto import for “Collectors” class
Step 1) Click on Window -> Preferences option menu
Step 2) In the preference window goto Java -> Editor -> Content Assit -> Favorites option
Step 3) on the favourites section click on the New Type button which is on the right. It will open a new dialogue window. Enter the class name “java.util.stream.Collectors” and click on ok button.It will add the
Step 4) It will add the corresponding class to favourite section.Now click on “Apply and Close” button.
Step 5) Now Lets go to actual program window, let’s try again for content assistant feature by pressing “ctrl+ space” button by entering few letters of method. Now we will see “toList()” method as suggestion.
Step 6) If you select the “toList()” method, it will automatically add the static import in import section.
Once you add the class with static methods to favourites window , static methods from that class will be included in the “regular” code completion (Ctrl + Space).
Autocompletion options are available in other class files with in the workspace.
Enabling static auto import in Intellij Idea
For enabling static auto import, we do not need to change any setting in Intellij Idea.But we need to follow certain steps.
Once again I am trying to show the code example using static methods are from “Collectors” class.
Step 1) Write the code in the editor and write a few letters of the static method you want to use
Step 2) Now try content assistant feature by pressing ( CTRL + space (twice)) to see the method suggestions.
Note : You need to hold CTRL button and press space button twice. With single press you will not see any suggestions.
Step 3) IDE will add static method call in old fashion way (i.e invoking static method on Class)
Step 4) Now place the cursor on the static method and press ALT +Enter button combination. IDE popups small window with different options.From the options choose “Add static import for XXX” option
Step 5) Now IDE will change code to static import
Note: once we’ve statically imported a single method from a class (Collectors.toMap), other static methods from that class (like Collectors.summingInt) will be included in the “regular” code completion (Ctrl + Space)
If you want to import all static methods from a calss using wild card entry like “import static java.util.stream.Collectors.*”. Place the cursor on the class and press ALT + space then IDE will show a small popup with options. From the options select “Add on demand static import for XXX”
The biggest disadvantage with Intellij Idea approach is you have to follow steps in every class file