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 ?
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.
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With