Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is importing only the necessary files from package advantageos???

 
jyothi lakshmi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I include only specific(required) files from a package have any advantage over importing all the files from the package?
Thanx in advance.
rgds
jyothi
 
sanjays samadder
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
an import statement does not bring all the classes in that package.it inludes only the ones used.
e.g if you import
import java.appelt.*;
instead of java.applet.Applet;
it does not make a difference.
both the statement bring only those classes that you use.
this is as far as I know.
regards
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jyothi,
The only advantage of importing specific classes instead of all classes in a package is that if both packages contain classes with the same name you will not have to always refer to them explicitly.
i.e. Both java.awt, and java.util have a List class ( well, the util one is an interface, but they are both called List ). If you needed to use classes from both packages, but not both java.awt.List and java.util.List ( for example, you use some awt components, but not the List awt component, and you use the java.util.List collection ) then you would do this :

If you imported them like this :

You would have to explicitly refer to List as java.util.List everywhere in your code.
Of course, if you are actually using both components, you have to do this anyway... i.e. you are using a java.awt.List component to display the contents of a java.util.List.
Fortunately, there are not many classes in the Java APIs that have a name collision like this ( in fact, java.awt.List and java.util.List, & java.sql.Date and java.util.Date are the only ones that come to mind. ), so this will not come up too often.
There is no other impact that importing has on your code. It is not like the C #import statement where code is appended to your cpde, the Java import statement just tells the compiler how to resolve references.
HTH,
-Nate
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another advantage... you document which classes you are using right at the top of your class. Many shops forbid the use of "*" for just this reason.
------------------
Moderator of the JDBC Forum
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic