Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive import Java

Tags:

java

I am a newbie to Java development. I have a quick question regarding recursive imports in Java.

Lets assume Package "pkg" contains the following

  • class A,
  • package B (This in turn contains B1 class)
  • package C (This in turn contains C1 class)

If I use import pkg.* why am I not allowed to import classes from packages "B" and "C" ?.

I would like to understand the rationale behind in Java's not allowing me to do recursive imports.

like image 368
UnderDog Avatar asked Aug 15 '13 02:08

UnderDog


2 Answers

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 named oliver.twist, or between packages named evelyn.wood and evelyn.waugh. That is, the code in a package named oliver.twist has no better access to the types declared within package oliver 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).

like image 63
Ted Hopp Avatar answered Sep 18 '22 11:09

Ted Hopp


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.*;
like image 21
sgbj Avatar answered Sep 18 '22 11:09

sgbj