Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import declaration

Tags:

java

I'm reading the Java 8 specification to better understand the Java language.

Specifically, the Chapter 7 Packages.

However in 7.5.2 7.5.2 Type-Import-on-Demand Declarations I don't understand the case where we can use TypeName according to the following syntax:

import PackageOrTypeName . * ;

The specification says:

If the PackageOrTypeName denotes a type (§6.5.4), then the name must be qualified (§6.5.5.2), or a compile-time error occurs.

So I jump to 6.5.4 but the following is very unclear:

If the PackageOrTypeName, Q, occurs in the scope of a type named Q, then the PackageOrTypeName is reclassified as a TypeName. Otherwise, the PackageOrTypeName is reclassified as a PackageName. The meaning of the PackageOrTypeName is the meaning of the reclassified name.

So I can't imagine how to use TypeName.

Please, can anyone provide me with an example to import types with * with TypeName?

like image 589
xdevel2000 Avatar asked Mar 13 '17 13:03

xdevel2000


1 Answers

You seem to be forgetting about nested classes. An example would be:

import java.util.Map.*;

Which would import Entry. It can now be used like this (unqualified):

Entry<String, String> e = ...;

Another way to do this import is:

import java.util.Map.Entry;

Where java.util.Map is the name of a type.

like image 57
Jorn Vernee Avatar answered Nov 11 '22 06:11

Jorn Vernee