Shouldn't “import foo. *” Also include the subpackage “foo.bar. *"?

Learning Java, I thought of myself, a rather confusing property of many tutorials. Consider two examples of import from a sample tutorial:

import java.awt.*; import java.awt.event.*; 

The first line explicitly imports the java.awt package, and the second is a subpackage. But should an asterix include all subpackages? Therefore, line one should do the trick - two lines are not needed? If this is not so: then what is the true purpose / use of asterix?

For example, using SELECT * FROM foo in MySQL selects ALL fields from a table, maybe I'm stupid to assume that this is, of course, the case.

+7
source share
3 answers

No, packages are generally accepted. Although it is often useful to think of them hierarchically, there is no concept in Java or compilation that says java.awt.event belongs to java.awt .

Your comparison with SQL tables is not entirely correct, because in SQL databases there is no such thing as a subtable. Instead, imagine that you have a table representing all your classes, with the following data:

 ID | Package | Name -------------------------- 1 | awt | SomeClassName1 2 | awt.event | SomeClassName2 

Now, if you want to get awt classes, you would say:

 SELECT * FROM MyTable WHERE Package = 'awt' 

You did not expect this to give you both entries, just because the package name starts with awt , could you?

+13
source

See my answer here (and my comment). The wildcard goes only to level 1, not subpackets.

0
source

Each package name ("abc") is a discrete package. For example, two packages with a similar naming prefix ("abc" and "abd") are separate packages for the language, both for namespaces and for access control.

For people, of course, this represents a possible connection between packages.

0
source

All Articles