Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Load package or class?  RSS feed

 
Nick Way
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted an idea of metrics as to when you should load a whole package, and when to load several individual classes from that package. Is there a big reason not to load a whole package if it's as big as, say, java.awt?
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loading all the classes from a package is not going to affect the performance in anyway.
Personally, I have a thumb rule. If I need to import more than 4 classes from the same package, I do a import *.
For anything less than 4, I import them seperatly.
It is just a matter of personal preference & coding standards practiced.
 
Dan Lastoria
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler determines which classes get compiled with your class depending on which ones you use. So an import statement like:
java.sql.*;
Is no less efficient than importing the specific class you are using.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you say
import javax.swing.*
no class is loaded at all.
Classes are loaded when they are first needed.
You only affect the visibility of names ('JButton').
So if you have an own class 'JButton', you may not use the '*', but have to fully classify every class of swing you use.
A minor issue is the time, the compiler needs to search for classes, because it may or may not be in an '*' package.
But my experience tells me, that you don't feel the difference.
You can try the following out:
Change an import statement to the '*' notation, and recompile.
Then compare the generated classes.
I assume, they are bitwise identically.
Are they?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!