29

I've tried finding an answer to this both online and in my own set of knowledge, but I cannot seem to find a definitive, clear answer.

Suppose I'm using only one class from another package only once which needs to be imported, say myPack.anotherPackage.ClassName.

What is the difference, if any, between using an import statement:

import myPack.anotherPackage.ClassName;

versus using a fully qualified name:

myPack.anotherpackage.ClassName classInst = new myPack.anotherpackage.ClassName();

?

Obviously this question only applies if ClassName is only used once.

3
  • 1
    looks like a duplicate of stackoverflow.com/questions/5125404/… Commented Aug 10, 2012 at 18:34
  • 1
    @AaronKurtzhals Not really. The question you pointed out asked about the performance of using import statements, while this question is more about the difference between using import versus using a qualified name. Answers would be different. Commented Aug 10, 2012 at 18:36
  • What additional information are you looking for that is not covered by aioobe's accepted answer in the question I linked to? Commented Aug 10, 2012 at 18:45

7 Answers 7

28

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.

Sign up to request clarification or add additional context in comments.

Comments

11

None, in my opinion. Look at the bytecode - there will be no difference.

 javap -c <your class>

2 Comments

What about at compilation? Is there any difference?
@NathanSabruka none that I know of
7

1. Its more about the readability value than of functional importance.

2. import statement provides a cleaner code than using the full package name along with the class or interface name.

3. But you must take care during importing packages which are ambiguous like.

  `java.awt.List` and `java.util.List`

Then you need to import one fully and one with full package name, like below

import java.util.List;

and 

java.awt.List l = new java.awt.List();

1 Comment

I'd say you need to import at most one fully. I've used the FQN for both if I feel it makes the code more readable. For instance, if I have a method that converts between java.util.Date and java.sql.Date, I'll fully qualify both to make it clearer to the reader what's happening.
3

There is no difference after code is compiled. In both case byte code will have fully qualified names i.e. class names are replaced with qualified names by compiler automatically. Its just to make programmers life easy, where we don't have to write fully qualified name to refer to a class.

Comments

1

There is no real difference, especially for your specific simple case cited here. See Java: Use import or explicit package / class name?

Comments

1

The import and package are used for naming and access protection (default) Simple reason for this is there can be many classes with same name.

In order to differentiate between them we need to separate them under packages and import these when required.

There is no difference in importing a class or directly typing fully qualified name, however there are some minute differences. 1. Most importantly if you import all classes in package (i.e. import xyz.*;) The classes can still have naming problem for example if there are multiple general imports you may be using wrong class at runtime.

  1. In case of dynamically loading classes at runtime for example JDBC driver registration at runtime, you need to provide full qualified classname as parameter so it can be loaded dynamically.

In general use following best practice: 1. Never use import abc.*; format of import and always import individual classes (eclipse or some IDE can auto correct imports to * and need to disable this) 2. You have to use fully qualified class name when you have already imported a class of same name and want to use another class with same name under same program. 3. Hope it helps :)

Comments

0

From my knowledge no difference. The only difference is the verbosity of your code. Generally the more verbose the harder it is to read. Perhaps looks at the bytecode?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.