Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't "import foo.*" also include subpackage "foo.bar.*"?

Studying Java, I've thought about a, to me, rather confusing property of many tutorials. Consider the following two imports from a sample tutorial:

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

The first line obviously imports the java.awt package, and the second one awt's subpackage. But shouldn't the asterix include all sub-packages? Hence, line one should do the trick - line two shouldn't be needed? If it doesn't: then what's the true purpose/usage of the asterix?

For example, using SELECT * FROM foo in MySQL selects ALL fields from a table, perhaps I'm stupid to assume that this naturally is t he case.

like image 618
Zar Avatar asked Aug 26 '12 15:08

Zar


1 Answers

No, packages are taken as a whole. Even though it's often useful to think of them hierarchically, there is no notion within the Java language or compilation that says java.awt.event belongs to java.awt.

Your comparison with SQL tables isn't quite right because there's no such thing as a sub-table in SQL databases. Instead, imagine you had a table representing all your classes, with the following entries:

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

Now, if you wanted to get awt classes, you'd say:

SELECT * FROM MyTable WHERE Package = 'awt'

You wouldn't expect this to give you both entries, just because the package name starts with awt, would you?

like image 156
StriplingWarrior Avatar answered Sep 21 '22 06:09

StriplingWarrior