How do you import static in Eclipse?

Home  >>  Eclipse  >>  How do you import static in Eclipse?

How do you import static in Eclipse?

On March 28, 2012, Posted by , In Eclipse, By , With 13 Comments

Long story short, I just discovered that Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports. It can also help with static imports. Executed on a reference to a qualified member (read Class.member), the refactoring will add a static import for the defining class and remove the class-dot expression.

For example, if you have

import java.lang.System;
class Example {
  void someMethod() {
    System.currentTimeMillis();
  }
}

Place the cursor on currentTimeMillis() and press Ctrl+Shift+M. This will transform the code to

import static java.lang.System.currentTimeMillis;
class Example {
  void someMethod() {
    currentTimeMillis();
  }
}

This feature has probably been here for a while and is documented and was announced in a New & Noteworthy. I only just discovered it the other day and found that it highly improves working with static imports. Maybe you find it useful too…

The long part of the story…

Tired of often manually managing static imports and encouraged by the new API for quick assists in JDT, I started writing a quick assist. This quick assist should replace qualified calls to static methods with static imports.
Only after I already had a mostly working implementation, I searched the JDT Bugzilla for such an enhancement request to eventually provide a patch. Thereby I discovered that Ctrl+Shift+M already does the job.
In the end, I spent an evening coding and got to know new APIs of JDT, learned a new feature of an old shortcut and was remembered to “think first, then act”. 🙂

Extras for Eclipse

Extras for Eclipse

Install Extras for Eclipse to get the most out of your Eclipse IDE.

Extras for Eclipse is a collection of small extensions that include a launch dialog, a JUnit status bar, a launch configuration housekeeper, and little helper to accomplish recurring tasks with keyboard shortcuts. Learn more...

Drag the Install link below to your running Eclipse IDE

Drag to your running Eclipse workspace to install Extras for Eclipse

Or select Help > Install New Software... from the Eclipse main menu and install from this software repository https://rherrmann.github.io/eclipse-extras/repository/

Please note that a JRE 8 or later and Eclipse Luna (4.4) or later are required to run this software.

Follow me

Rüdiger Herrmann

Routined programmer, team lead, presenter, blog writer, and convinced open source contributor with two decades experience in the field.

Strong focus on quality backed up by agile methods like test driven development, modularization, pair programming, clean code, continuous integration.

Specialized in Java with JEE, REST, OSGi, RCP, RAP and building developer tools based on the Eclipse IDE (or any other platform if an opportunity arises).
Follow me

Latest posts by Rüdiger Herrmann (see all)

13 Comments so far:

  1. Matthieu Brouillard says:

    Cool hint!

    Another way also of dealing with frequent static import is by defining “available” static imports for content assist.
    You can do this in > Preferences > Java > Editor > Content Assist > Favorites.
    In my preferences I always have : org.junit.Assert.* and org.hamcrest.CoreMatchers.*.
    Then writing test, I can type: assertTh + CTRL-SPACE, and then eclipse will propose me directly the assertThat method and import static it.

  2. Mike says:

    Great stuff! Adding each class to favourites is a pain in the backside.

  3. MG says:

    Cool tip. Thanks!

  4. Ashutosh says:

    Great find !

  5. Kevin Clarke says:

    Thanks! Not being able to do this has always bothered me. Glad I’ve finally learned the trick!

  6. Tim says:

    Extremely helpful! Coming from IntelliJ and now using Eclipse again, I am glad to have found your tip here for Eclipse!

  7. Awesome! Helped a lot, thanks :-).

  8. Just in case anyone else has the same awkward behaviour that I had, here’s a bit more info…

    Working with the MongoDB driver, I typed the line:

    collection.updateOne(eq(“x”, 5), Updates.set(“updated”, true));

    “Updates” was highlighted (cannot be resolved)

    With my cursor on “set”, pressing Ctrl+Shift+M did nothing.

    Ctrl+Shift+O was required first. This imported (non-statically) com.mongodb.client.model.Updates. But the line still had “… Updates.set …” in it.

    I then used Ctrl+Shift+M with the cursor on “set” and it created the correct static import and removed the “Updates.” from the line.

    Cool!, But… the original import for the Updates class was still there, so I needed to do another Ctrl+Shift+O to clean that up.

    A bit of a pain, but still quicker than having to type it all manually 🙂

    Hope that helps someone.

    • Rüdiger Herrmann says:

      Thanks for sharing your findings, Jim.

      The static import command in its current form can only convert an existing import into a static import. Therefore, you need to import the required package first. If you hit Ctrl+Shift+M first on Updates and then on set, the first invocation creates a regular import and the second command will convert it into a static import.

      However, importing would be even more fun if JDT would find the class to import static by itself.

      In any case JDT should be smart enough to remove the original import if it isn’t needed anymore. Did you consider filing a bug report?

Leave a Reply

Your email address will not be published. Required fields are marked *