Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse : transform static method invocation to a static import

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 » Favoritesas 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".

like image 627
gontard Avatar asked Mar 22 '13 09:03

gontard


People also ask

How do I add a static 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.

Can you import a static method?

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.

When should you import static?

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.

Can we use static after import in Java?

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.

How do I import a static method in Eclipse?

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.

What is static 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.

How to add a static import to optimize imports?

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.

How to access the static members of a class directly?

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.


1 Answers

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.

like image 138
Martin Ellis Avatar answered Oct 09 '22 09:10

Martin Ellis