I am a newbie to Java development. I have a quick question regarding recursive imports in Java.
Lets assume Package \"pkg\" contains the following
There is no restriction on imports as you described in your original question (prior to the edit).
Let's assume you have the following Hierarchy:
pkg1
A.java
pkg1.pkg2
B.java
pkg1.pkg3
C.java
D.java
You are permitted to import any of the A.java, B.java, C.java, D.java, or combinations therein.
import pkg1.A;
import pkg1.pkg2.B;
import pkg1.pkg3.C;
import pkg1.pkg3.D;
The only caveat is that within class C, you can use class D without importing D explicitly because they share the same package.
However, a blanket statement like import pkg1.*
will not pull in the classes located further down the package hierarchy; you'd need to import each sub-package as well: import pkg1.pkg2.*
, etc. This is simply how the language is designed.
Your question is poorly phrased, because if you import pkg.*
, you certainly are allowed to then import classes from packages pkg.B
and pkg.C
. That is, it is perfectly fine to do this:
import pkg.*;
import pkg.B.*;
import pkg.C.*;
But I assume that what you really are asking is why, if you import pkg.*
it does not automatically import the types declared in the subpackages of pkg
. To answer that, it's best to turn to the Java Language Specification:
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.
In other words, when you import pkg.*
, you are importing all the top-level types defined by the compilation units contained in the package named pkg
, but you are not importing any of the sub-packages of pkg
(such as pkg.B
or pkg.C
).
It sounds like you're asking why import java.awt.*
doesn't also import java.awt.color.*
and so on.
This is best explained by this tutorial where it says
At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.
More importantly,
Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:
import java.awt.*; import java.awt.color.*;
The convention in Java is to import only the classes you required. Most IDEs will automatically organise them for you. Importing classes in bulk is not common like in other languages.