Originally posted by Manju Devarla:
if the options are
import com.name.*; and
import static com.name.xyz.*;
which one to select..? (only one option to select)
These do different things, so it depends on what you want to do.
import com.name.*; makes everything in the com.name package visible using simple (unqualified) names. For example, if you wanted to implement the xyz interface
without the import statement, you would need to use a qualified name of "com.name.xyz." The import statement opens the namespace, and allows you to simply say "xyz" instead.
Note that the wildcard * opens the namespace to include everything in the package. So it's not always clear to another reader why the import statement is there, and it can cause conflicts. For example, if com.name also included a class called abc, and your code defined a different class called abc, then there would be a clash of names when you said "abc." To avoid this, it's often better to import only the specific type you need: import com.name.xyz;
import static com.name.xyz.*; makes all the
static members of xyz visible using simple (unqualified) names. For example, if you wanted to use static members of xyz
without an import statement, you would need to use a qualified name of "com.name.xyz.d1." Even if you had imported the interface with
import com.name.xyz; you would still need to qualify the variable name as xyz.d1 (unless you had already implemented the interface, which would make the variables available). The static import allows you to simply say d1 even without implementing the interface.
Note that the static import only makes the static
members visible -- it does not make the class or interface itself visible. So
import static com.name.xyz.*; does
not allow you to say "implements xyz" because only the static
members of xyz are visible -- not xyz itself. And here again, the wildcard * can cause confusion and/or conflicts.
So the bottom line is this:
If you want to extend the class or implement the interface, then use a non-static import (import com.name.*; or better yet import com.name.xyz;).If you only need to use the static members of the class or interface, then use a static import (import static com.name.xyz.*; or better yet import static com.name.xyz.d1;). [ December 16, 2006: Message edited by: marc weber ]