java.util.ArrayList x = new java.util.ArrayList ( 149 );The alternative to this long-winded style of coding, is to use import statements. A typical set of import statements
import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.Properties;They must come right after the package statement, before the class statement. They are traditionally kept sorted in alphabetical order. Then you can code in an
ArrayList x = new java.util.ArrayList( 149 );
Unlike C or C++ we do not need to include headers to help the compiler determine what sorts of parameters other routines want; it can go look for itself in the source or class files. The import statement is not like the C++ include. So long as you fully qualify your reference in the code to class names with com.mindprod.mypackage.myClass there is no need for imports. They just allow shorthand. Even when you do have an import, you can still fully qualify your references to classes.
It is a great help to understanding someone else’s code, (or even your own), if you refrain from using the .* style of import with the imports giving you an explicit list of the classes you use in that class. The list of explicit imports gives you a rough idea of what the class could do and what it is likely up to. Don’t leave unused imports lying around. Use an import tidier, or Eclipse, to get rid of them, and sort the valid ones into alphabetical order. During development, it is fine to use the lazy .* form, (doing so will save you a ton of compiler errors), but once your work gels, convert over. Bon’t camouflage your use of classes by fully qualifying them and not reporting them in the import. The only time you need qualification is when there is a name clash, e.g. java.awt.List and java.util.List.
The other problem with using wildcards is this. Let’s say you use import java.awt.* to get your Label class. Then later somebody using your code has a com.wombat.superawt. Label class on their classpath. The code won’t compile, or worse, it will use the wombat Label class in place of the awt Label class without telling anyone.
|
|
You can get the freshest copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) |
| http://mindprod.com/jgloss/import.html | J:\mindprod\jgloss\import.html | |
![]() | Please email your feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording,
broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Canadian Mind Products | ||
| mindprod.com IP:[65.110.21.43] | ||
| view Blog | Your face IP:[38.107.179.213] | |
| Feedback | You are visitor number 176,404. | |