Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a Type-Import-on-Demand Declaration import?

Tags:

java

import

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?

like image 479
John Assymptoth Avatar asked Dec 09 '22 11:12

John Assymptoth


1 Answers

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?

like image 142
Turtle Avatar answered Dec 20 '22 05:12

Turtle