How to do static import in Eclipse - Java

Do you know what is shortcut of doing static import in Eclipse? Well I didn't know before, but today I come to know that shortcut Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports but It can also help with static import in Java program.  Suppose you are using lots of static variable from a utility class e.g. TimeUnit by referring them with class name, just like we refer static variable. In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.
For example, if you have following code in your class, you can select TimeUnit.SECONDS and then use shortcut Ctrl+Shift+M to statically import SECONDS variable in your program, as shown in first and second screenshot.

import java.util.concurrent.TimeUnit;

/**
 * Java Program to show how you can static import some class variables.
 * 
 * @author WINDOWS 8
 */

public class Test {   
    
    public static void main(String args[]){
        
        System.out.println(TimeUnit.SECONDS); 
        System.out.println(TimeUnit.MINUTES);
        System.out.println(TimeUnit.DAYS);
          
    }
   
}
How to import static variable in Eclipse



As shown here, Just highlight or select TimeUnit.SECONDS and type Ctrl+Shift+M or choose Menu option Add import to static import this static variable from java.util.TimeUnit class. By doing this three times in this program you can reduce above code into following code, also shown in fourth screenshot.

Eclipse Shortcut for doing Static Import in JavaStatic Import in Eclipse

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.concurrent.TimeUnit;

/**
 * Sample program to demonstrate Eclipse shortcut for doing static import.
 * 
 * @author WINDOWS 8
 */

public class Test {   
    
    public static void main(String args[]){
        
        System.out.println(SECONDS);
        System.out.println(MINUTES);
        System.out.println(DAYS);
          
    }
   
}

importing static variable in Java in Eclipse

By the way this feature is not that seamless e.g. it will not work if import to TimeUnit class is missing i.e. using Ctrl+Shift+M will have no effect if you have not imported java.util.concurrent.TimeUnit class already. Only after having this import in your code, you need to select member and press Ctrl+Shift+M to static import that field or method. Here also you cannot import all static members in one shot, you need to first select each of those element and then executing that shortcut for as many time as number of static members.

8 comments :

SARAL SAXENA said...

@Javin ...good one this will save the time of Developers ... !! :) just to add for a background with context to static import ...

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). ... If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually

Gordon said...

Thanks - very useful tip!

Out of curiosity, do you know which version of Eclipse this was first implemented? It's been around since long?

Anonymous said...

I have one question. What is the advantage to do a static import instead of a classical import?

Javin Paul said...

@Anonymous, main advantage of static import over classical import is that you can use the variable as it belongs to same class e.g. instead of writing TimeUnit.DAYS.sleep() you can just write DAYS.sleep(). This reduces your code and make it more readable.

Anonymous said...

There is a misconception that static import should be declared before normal import, this is not true. You can declare static import statement even after normal import statement, compiler will not complain, as shown below :

import java.util.*;
import static java.util.concurrent.TimeUnit.DAYS;

Anonymous said...

@Gordon, Sorry don't know which Eclipse version it was first implemented, but yes it's look it was there from long time.

Anonymous said...

Eclipes always amaze me, there is so much to learn and know about the IDE which I use daily. Though I am not big fan of static import and hardly use it ever I find it interesting how Eclipse support even minor feature. Please write aobut code quality, debugging and code reading skills.

Anonymous said...

good explanation with great summary ....great job ..... thank you.

Post a Comment