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 import statements in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import statements in java" Watch "import statements in java" New topic

import statements in java

gokulanand narayanan

Joined: Feb 16, 2006
Posts: 23

Which will be the right way of importing the classes.

1. import java.sql.ResultSet;

2. import java.sql.*;

but i need only the ResultSet class from the package.

is there any advantages using import like (point 1).

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621

Both the ways of importin classes are correct
you can use both,but as you said you want only
ResultSet class so the first option is better....

And now about the advantage of using a specific class
from the package

like import java.sql.ResultSet;

If you use such it implies the user
of your code that which specific classes from
the package java.sql you have used in your
code,the other user gets a easy understanding
of your code bcoz if you import complete
package ,if anyone jumps into your code
wont understand which classes from the package you have utilised...
and one more thing to say it shows your understanding as well

i hope it helps........

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113

If you know that you are going to import only that particular class of that package then the first line is enough.Else you can use the second.Advantage is first line if you know the class that to be imported.Advantage Because its mere asking the JVM to search all the thing when you know the required.Thats it !!!

I hope you get it !!!
Sampath Vinay Kumar

Joined: Jun 30, 2007
Posts: 3

i think it is also an overhead at the compile time to import all the classes in java.sql, when you know which class you want to import specifically. it also reduces your compile time.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I'll bet that any impact on compile time will be far too small to be measured, much less noticed by a human being.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
gokulanand narayanan

Joined: Feb 16, 2006
Posts: 23

Thanks for all.
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
Well as said above, Both the ways of importing classes are correct!

But to improve the readability of your code and not leave the user wondering for "How many other classes could Programmer have imported with that wild card character(*) other than the specific classname used?"

It will just tell the user directly that "Look i need to use this specific class and for that I'll have to Import this class only.Why go on adding its neighbours and relatives unnecessarily when you just need that one whole thing?"

As far as the compile time is concerned it makes no visible effect on it when you go for small applications but when you go for HUGE web applications it does take its toll on the compile time.

So the crux is if you are 128% sure which class you are going to need and use then go ahead with calling that class by else*

Hope it helps...All the very Best!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I just ran across a class with an ambiguous import error. There were two imports with stars that could lead to a class called X. On the first day this was written it was probably ok as X existed only in package 1. Then somebody added a new class X to package 2 and the code broke. Ouch! Don't use them stars.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: import statements in java
It's not a secret anymore!