In Java, there are two valid forms of the import
declaration:
import java.lang.Math;
import java.lang.Math.*;
In the latter, a wildcard is used. This form is known as a Type-Import-on-Demand declaration, but how is it different from the former? Does it also import the subpackages of java.lang.Math
?
What if Math
were a Type (e.g., a class)—would all of its inner classes be imported?
Only immediately-nested types are imported. The declaration is not recursive.
This does work with types for importing inner classes.This also works with static import (for importing methods).
import static a.b.c.FooBar.*;
The documentation states:
Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
So importing import java.lang.Math.*;
will not import the Math
class.
NOTE: You may also want to see Why is using a wild card with a Java import statement bad?
import java.lang.Math.*;
This will import all nested classes declared in the Math
class in the java.lang
package. References to nested classes could be given without the outer class (e.g., Foo
for java.lang.Math.Foo
).
import java.lang.Math;
This will import the Math
class in the java.lang
package. References to nested classes would have to be given with the outer class (e.g., Math.Foo
).
The statement
import java.util.ArrayList.*;
imports all nested classes of ArrayList, but not ArrayList itself. Since ArrayList does not have any (public) nested classes, the statement actually does nothing.
However, consider the interface Map
, which defines the nested class Map.Entry
. If we write
import java.util.Map.*;
at the start of the Java file, we can then write Entry<A,B>
instead of Map.Entry<A,B>
to refer to this nested class.
Importing members of classes usually makes the most sense if you are using static imports. Then you don't import nested classes, but static methods and variables. For example,
import static java.Math.*;
will import all static constants and methods from the Math
class. Then you can use the static methods of the Math
class by writing, e.g. sin(x)
instead of Math.sin(x)
.
With the statement import java.util.ArrayList.*;
you will import all nested classes declared into ArrayList
class.
If you also want to import methods and const, for example, declares:
import static java.lang.Math.*;
Then you can use the constant PI
in your code, instead of referencing it through Math.PI
, and the method cos()
instead of Math.cos()
. So, for example, you can write:
double r = cos(PI * theta);
Basically Math is a final class, and it does not have further sub classes. There is no difference between import java.lang.Math.*
and import java.lang.Math
Both are one and the same. So I really dont see the need here to use the first kind of import.