Importing Java packages and classes

I'm talking about the stuff we import.

Suppose, there is something like:

import java.util.Scanner;

util is the package and Scanner is the class, right?

Can this be always generalised and be said that in the import statement, the first one would be java/javax (is there any other?), the second one package, the third class and the fourth, if any, will always be a method belonging to the class?

Are there any exceptions, if so please name them.

Jon Skeet
people
quotationmark

Can this be always generalised and be said that in the import statement, the first one would be java/javax (is there any other?), the second one package, the third class and the fourth, if any will always be a method belonging to the class?

Not really, no.

The package is java.util, and you'll definitely see other packages, including the ones you write.

Within a normal import statement, there'll be a package, then either a specific class name or a * (to import all the classes in a package). But the class name could be a nested class name, so you could have:

import foo.bar.Outer.Nested;

where foo.bar is the package name, and Outer.Nested is the class name.

Then there are static imports, where you import members of classes, e.g.

import static java.lang.Math.sqrt;

See the Java tutorial on imports for more details.

people

See more on this question at Stackoverflow