Java: the meaning of * used with import

The tutor offers an interpretation of what the asterisk (aka, wildcard) means in an import statement.

I’m not trained in Java, but do write experimental programs with it. Here are some ideas about the import statement, as I understand it:

Herein, it might be helpful to think of a package as a directory.

The Java statement

import pckg0.member0;

gives access to the definition of member0, as needed for the program. In this context, member0 is a single, specific class found in the package pckg0.

The statement

import pckg0.*;

gives access to all “single” classes contained in pckg0, but not to those of its subdirectories. If pckg0 contains the package pckg00, which contains the class mem01, then the statement

import pckg0.pckg00.mem01;

provides access to it.

The statement

import pckg0.pckg00.*;

provides access to all single classes in pckg00.

Therefore, the two import statements, one atop the other,

import pckg0.*;
import pckg0.pck00.*;

can sensibly occur together at the top of a Java program.

HTH:)

Source:

McGrath, Mike. Java in Easy Steps, 5th ed. Warwickshire: Easy Steps Limited, 2014.

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply