Reputation: 506
I'm confused about importing packages in Java.
When I need to import java.util.Scanner
, it is also done by the import statement like this: import java.util.*
(this makes sense to me, as the Scanner
class is in inside util
).
But this trick works for java.awt.event
and java.awt.*
. This makes me more confused. (If your answer is that Java packages are not nested, please tell me the reason why the above tricks works. And if Java has both nested and independent packages how i can find them?
Upvotes: 3
Views: 2713
Reputation: 42176
Import wildcards are not recursive.
Importing java.awt.*
does NOT import anything from java.awt.event
. It only imports the classes in java.awt
, not its "subpackages".
It should also be noted that importing a package doesn't actually do anything, it simply tells Java where to look for the class names you use.
Also, using wildcards is a bit sloppy anyway. You should probably be importing only the classes you actually use.
Edit: This question has been asked before: Recursive import Java and a quick google search of "java recursive import package" turns up a whole bunch of results, including:
At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a
java.awt
package, ajava.awt.color
package, ajava.awt.font
package, and many others that begin withjava.awt
. However, thejava.awt.color
package, thejava.awt.font
package, and otherjava.awt.xxxx
packages are not included in thejava.awt
package. The prefixjava.awt
(the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.Importing
java.awt.*
imports all of the types in thejava.awt
package, but it does not importjava.awt.color
,java.awt.font
, or any otherjava.awt.xxxx
packages. If you plan to use the classes and other types injava.awt.color
as well as those injava.awt
, you must import both packages with all their files:import
java.awt.*
; importjava.awt.color.*
;
And the JLS:
The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type (§7.6) declared in that package.
For example, there is no special access relationship between a package named
oliver
and another package namedoliver.twist
, or between packages namedevelyn.wood
andevelyn.waugh
. That is, the code in a package namedoliver.twist
has no better access to the types declared within packageoliver
than code in any other package.
Upvotes: 6
Reputation: 8191
Package is Java's way to organize class files. Import a parent package will just import classes present inside that package, NOT the sub-packages.
Thus, to conclude, java.awt.*
will import only java.awt.<Class>
and NOT java.awt.event.<Class>
There is not a performance or overhead cost to doing import <package>.*
vs importing specific types. However, it is considered to be a best practice to never use import <package>.*
Basic reason for this is to keep things straight, clean and with as little ambiguity as possible, and with a <package>.*
import you lose that.
Consider you have imported java.lang.reflect.*
& java.sql.*
, both the packages have Array
class. You will end up in ambiguous situation. Hence you should avoid that and import java.lang.reflect.Array
& java.sql.Array
individually.
Shishir
Upvotes: 2
Reputation: 8058
Java packages look like they are hierarchical, but they are not. Each package name is just a dot seperated word list which defines a namespace. All namespaces together are not hierarchical, but flat, all at the same level.
Hence the notation import a.b.c.*
means import all types defined by the package a.b.c
, while import a.b.c.Z
means import type Z
defined by package a.b.c
.
Upvotes: 0