If we are importing java.parentpackage.*;
...
- ... do child package classes also get imported or only the classes in parent package?
- ... is it required to import
java.parentpackage.childpackage.*;
to include all child package classes?
If we are importing java.parentpackage.*;
...
java.parentpackage.childpackage.*;
to include all child package classes?An import declaration which ends with a package name and .*
imports all public classes/interfaces/annotations of the package only and nothing else. If you need classes from the child package too, you have to import them separately.
If an import declaration contains a specific class, only that class will be imported.
If the import declaration contains a specific class plus .*
(it's a static import, must be in the form of import static ...
), then all static fields and methods will be imported from that class and nothing more.
Usually only specific classes are imported. It decreases the chances of class name collisions (2 different classes with same name defined in 2 different packages).
Also you are not required to import a class to use it, you can use a qualified name where every time you refer to that class, you always write full package name and class name.