Is there a way to transform automatically this static method invocation (Arrays.asList
):
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 this invocation using 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 know that i can configure the code completion using this Window » Preferences » Java » Editor » Content Assist » Favorites
as described in this answer.
My question is about transforming an existing static method invocation. Ideally, i would like do not have to configure a "favorite import".
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.
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.
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). In other words, use it when you require frequent access to static members from one or two classes.
With the help of import, we are able to access classes and interfaces which are present in any package. But using static import, we can access all the static members (variables and methods) of a class directly without explicitly calling class name. The main difference is Readability, ClassName.
Eclipse normally adds import statements for you when you autocomplete a type, press Ctrl+Shift+O or organise imports when you save. However, by default it doesn’t do the same for static imports, something you’ll miss when using classes like JUnit’s Assert and Apache Commons’ StringUtils.
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.
Window->Preferences->Java->Editor->Content Assist. and check the checkbox for Use static imports (only 1.5 or higher). This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (CTRL + 1) on the line it will give you the option to add the static import which is good enough.
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.sqrt (), but by using static import we can access sqrt () method directly.
Put the cursor on the method name (asList
) and press Ctrl-Shift-M
.
This is the default keyboard shortcut for the 'Add Import' command. You can also find the command on the 'Source' menu.
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