Ismail qouiqa 28 Jan 2020

Classes Imports

Often, you'll need a function of a Class that lives in a different package. in this case, you have to use the import statement

In the example above, In order to instantiate ClassB on line 5, we need to import ClassB from the packages.classes.imports.b package on line 3.

Someday, you will be tempted to import many classes located in the same package using this single statement :

It won't work, only the classes living inside packages.classes.imports.b will be imported, the nested packages inside won't.

Putting many asterisks in the import statement as follows doesn't also work

Note that only one Asterisk is allowed and should be at the the end of the import statement, therefore, to import multiple sets of classes,you will need multiple import statements.

you will often face import collisions when importing two classes that have the same name but live in different packages.The compiler doesn't know which one to choose and therefore fails.

Also beware of ambiguous import, in the example below, the code won't compile because Date class lives in both packages and the compiler won't know which Date Class to import.

Importing Classes explicitly takes precedence over wildcards, the following compiles successfully because java.util.Date Class won't be imported.