Java: Import Statement VS Fully Qualified Name

Difference between using fully qualified name and import in Java

The main thing you should focus in is readability. I find the second one more readable.

In rare cases, I prefer the second approach. Let's consider the following scenario: For some reason, I wrote a class and named it File. I typed File file = new File(...) and my IDE auto-imported the java.io.File for me. But I don't want that kind of object, I want my File class. So instead of importing the correct class, I prefer inline-import it, just that other users won't get confused with the Java's File class.

Regarding the performance, they're exactly the same, and here's the proof -

This is the bytecode generated for the first snippet:

public class java8.tests.General {
public java8.tests.General();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2 // class javax/swing/JFileChooser
3: dup
4: invokespecial #3 // Method javax/swing/JFileChooser."<init>":()V
7: astore_1
8: aload_1
9: new #4 // class java/io/File
12: dup
13: ldc #5 // String .
15: invokespecial #6 // Method java/io/File."<init>":(Ljava/lang/String;)V
18: invokevirtual #7 // Method javax/swing/JFileChooser.setCurrentDirectory:(Ljava/io/File;)V
21: return
}

This is the bytecode for the second:

public class java8.tests.General {
public java8.tests.General();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2 // class javax/swing/JFileChooser
3: dup
4: invokespecial #3 // Method javax/swing/JFileChooser."<init>":()V
7: astore_1
8: aload_1
9: new #4 // class java/io/File
12: dup
13: ldc #5 // String .
15: invokespecial #6 // Method java/io/File."<init>":(Ljava/lang/String;)V
18: invokevirtual #7 // Method javax/swing/JFileChooser.setCurrentDirectory:(Ljava/io/File;)V
21: return
}

Java: import statement vs fully qualified name?

Import statements make your code more readable, since you are not cluttering the code with the complete package.

In case if there is a conflict of ClassNames, then only in that case it is advisable to go for fully qualified names.

fully qualified name vs import statement

Based on my understanding of your question, you would like to use fully qualified name against import.

The reason you are getting error in first case is because you missed to mention fully qualified class name while doing:

ArrayList a=new ArrayList();

Each time you are declaring ArrayList object in this scenario, you have to use fully qualified name.

Reference : https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Full package-path versus import statement

There is no difference in the way things work. However in the case where you import java.sql.Date and then proceed to use it, you simply mean that whenever the Date class is used in this file, it is the class that is imported beforehand. If you don't import it and just use the Date class with its package name, you make no such assertions.

Importing and using just the class name is usually what you should do as the possibility of using classes with the same name is pretty low. In a case where you have classes with the same name in different packages, you may want to choose to not import them at all to make the distinction between the two classes more obvious.

Java: Use import or explicit package / class name?

There is no real difference; the generated byte code will be exactly the same. An import statement is really just a way to tell the compiler "when I write JFrame, I actually mean javax.swing.JFrame".

You might want to use fully-qualified package names if you have for example two classes with the same name in different packages. One common example from the standard library are the classes java.util.Date and java.sql.Date.

Intellij class Import being added to object not the top of class

See Preferences/Settings > Editor > Code Style > Java if this option ...

  • Use fully qualified class names

... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.

Here's a screenshot showing this configuration item:

Sample Image

Import package.* vs import package.SpecificType

There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.



Related Topics



Leave a reply



Submit