File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Regarding import statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding import statements" Watch "Regarding import statements" New topic

Regarding import statements

Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90

Which is the best practice while writting import statements?

Ex. I need only one class say File from package

Most of the time I do ...

import*; //Line 1

Is this a bad practise...Do I need to do like this always..??

import; //Line 2

What is the difference between Line 1 & Line 2..

Chandrasekhar S.

"Luck is when opportunity meets preparation"
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Obviously Line 2 approach is the best. JVM will be easily able to resolve the references avoid ambiguous references.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
What does JVM has to do with this? The compiler will throw error in case of any ambiguity.
Personally I prefer to import individual classes. But if I need to import more than 4 classes from the same package, I use the wildcard approach (import *).

Quaerendo Invenietis
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
In a small application it doesn't matter if you use wildcard import. In first place why do you want Compiler to resolve this ambiguous references and report back to you through an error message.

Isn't it good if we can avoid this extra step the JVM (compiler) has to take ?
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
The second import line will be clear to anyone who is taking over your codes. He/She would know specifically the classes that you'd imported.

SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Fully qualified import statements are always preferred.
Increasing code maintainability for no cost is a good thing.
Increasing code readability for no cost is a good thing.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: Regarding import statements
It's not a secret anymore!