Friday, August 13, 2021

How to do static import in Eclipse - Java Example Tutorial

Do you know what is a shortcut to 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 can also help with static import in the Java program.  Suppose you are using lots of static variables from a utility class e.g. TimeUnit by referring them with the class name, just like we refer to static variables. 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 the 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 the first and second screenshot.

Btw, if you are a beginner, I suggest you to first go through a beginner course like Eclipse Tutorials for Beginners to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.



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 the 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 the above code into the following code, also shown in the 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 elements and then executing that shortcut as many times as the number of static members.


Other Java Eclipse articles you may like to explore
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • How to remote debug Java applications in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developers should know? (see here)
  • How to attach the source code for the JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase the console buffer size in Eclipse? (steps)
  • How to use spaces instead of tabs in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)
  • How to Increase Heap Size of Java Program running in Eclipse? (guide)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.

8 comments:

  1. @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

    ReplyDelete
  2. Thanks - very useful tip!

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

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

    ReplyDelete
  4. @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.

    ReplyDelete
  5. 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;

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

    ReplyDelete
  7. 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.

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

    ReplyDelete