Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse remove a static import

With eclipse i can easily transform the static invocation to

import java.util.Arrays;
import java.util.List;

public class StaticImport {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello", "world");
        System.out.println(list);
    }
}

to a static import:

import static java.util.Arrays.asList;

import java.util.List;

public class StaticImport {
    public static void main(String[] args) {
        List<String> list = asList("hello", "world");
        System.out.println(list);
    }
}

I put the cursor on the method name (asList) and press Ctrl-Shift-M (Add Import).

Now, for some refactoring reasons, i want to remove the static import and get back to the first code:

List<String> list = Arrays.asList("hello", "world");

Is there a shorcut quickly do that ?

like image 552
gontard Avatar asked Jul 30 '14 10:07

gontard


People also ask

Are static imports bad?

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. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Can we use static after import in Java?

In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.

Why import option is not working in Eclipse?

Go to Preferences » Java » Editor » Content Assist » Advanced. Make sure Other Java Proposals is ticked/checked. If that does not work simply close the project and reopen it.

What is static import How is it useful?

Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.


1 Answers

You can't remove an (static) import statement with a shortcut, unless it's unused.

So, first comment out the statement:

//List<String> list = asList("hello", "world");

Then, activate the shortcut for Organizing Import Statements (Ctrl + Shift + O) and the unused import statements will be automatically removed.

Finally, uncomment the line you commented first and refactor it so it compiles:

List<String> list = Arrays.asList("hello", "world");
like image 157
Konstantin Yovkov Avatar answered Oct 02 '22 07:10

Konstantin Yovkov