Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statements in java

 
gokulanand narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?WildCardVsSpecificImports
 
gokulanand narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for all.
 
jenita brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic