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.
available on the web at:
optional Replicator mirror
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 : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[184.108.40.206]
|Feedback||You are visitor number 193,853.|