Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

Java: import statement vs fully qualified name?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...