Java packages, how to properly represent them and understand that they lack a hierarchy

By writing this import java.awt.*; I won't get the contents of this import java.awt.event.*; though it would seem I should, given that there is a hierarchy of packages. It seems that everything is packaged in packages, in one package, there are still nested packages. But when I get access to a package that has nested packages, I don't get the contents of the nested packages. I want to understand whether this is due to some special approach to structuring packages, or whether they are really in a hierarchical arrangement, but the system does not give access to nested packages if the main package is requested?
import java.awt.*; will it give me the contents of import java.awt.event;(note there is no asterisk in the second import)?
Or is event also a package, and import java.awt.*; will give me all the classes at this level, but not the packages? It turns out like in Windows: I applied something to a folder, and there is a question: apply to subfolders? It seems that there is a hierarchy, but the influence on it is controlled.

Author: Turalllb, 2018-04-16

2 answers

Java treats each package as independent. For example, local packages do not extend to any " sub " packages. I suspect that using the hierarchy in a meaningful way would be valuable, but the design of Java was to make everything as simple as possible.

Java does not treat packages as really sub-classifying each other; while java.util and java.util.concurrency may look like the second part is part of the first, however they are are treated as completely independent, and the dot is mainly used for neatness.

The reasons for this decision most likely stem from the general tendency of Java to be simple. Best practice is often to never use import wildcards at all.


According to the JLS specification, Section 7.5, there are only 4 ways to import:

A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).

Single import by its "canonical" name.

For example: import java.util.List;

A type-import-on-demand declaration (§7.5.2) imports all the accessible types (§6.6) of a named type or named package as needed, by mentioning the canonical name of a type or package.

Import all available types or packages by their "canonical" name.
This implies that there will be all child package names are imported, but not their contents.

For example: import java.awt.*;

A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a type, by giving its canonical name.

A single static import that imports all the static members of a package.

For example: import static org.junit.Assert.assertEquals;

A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as needed, by mentioning the canonical name of a type.

Import all static package members.

For example: import static org.junit.Assert.*;

Packages allow you to give the same name to different classes. If it were possible to import with * the entire sub-hierarchy of packages, there would be confusion in your local class names.


Association: why doesn't Java have "deep" wildcard import?, Java import all from all

 5
Author: Peter Samokhin, 2018-04-16 14:12:49

There is no package hierarchy. There is a (pseudo)hierarchy of names for convenience. Packages themselves are separate, even if a package is in a subfolder of another package, it is not nested.

Section Apparent Hierarchies of Packages in the original source:

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.

Translation:

At first glance, it seems that the packages are hierarchical, but this is not the case. For example, the Java API includes the java.awt, java.awt.color packages, java. awt. font and many others that start with java. awt. But neither java. awt. color, neither java. awt. font, nor any other java.awt.xxx package is included in the java.awt package. The java.awt prefix (Java Abstract Window Toolkit) is used for a number of packages to show that they are related (by scope), but not to show nesting.

 2
Author: Эникейщик, 2018-04-16 14:19:55