This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import statements in java" Watch "import statements in java" New topic
Author

import statements in java

gokulanand narayanan
Greenhorn

Joined: Feb 16, 2006
Posts: 23
Hi,

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).


thanks
gokul
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!

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
Hi,

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
Greenhorn

Joined: Jun 30, 2007
Posts: 3
hi,

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.

Regards
Sampath
Ulf Dittmer
Marshal

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


Ping & DNS - my free Android networking tools app
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://faq.javaranch.com/view?WildCardVsSpecificImports


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
Greenhorn

Joined: Feb 16, 2006
Posts: 23
Hi,

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 xxx.xxx.classname else xxx.xxx.*



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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: import statements in java