Ebony Maw
Ebony Maw

Reputation: 524

A wildcard is said to import all classes in a package. "It doesn't import child packages, fields, or methods." What does this mean?

In the Sybex book, OCA Oracle Certified Associate Java SE 8 Programmer I - Study Guide, page 10 of Chapter 1 states the following:

The * is a wildcard that matches all classes in the package. Every class in the java.util package is available to this program when Java compiles it. It doesn’t import child packages, fields, or methods; it imports only classes. (Okay, it’s only classes for now, but there’s a special type of import called the “static import” that imports other types. You’ll learn more about that in Chapter 4.)

It was my naive understanding that since a class contains members (fields and methods), it is implied that those are imported, as well. However, according to the author of this book, it appears that the situation is more caveated.

If you are importing a class, and you don't have access to the members of that class, then what is the point of importing that class?

Upvotes: 3

Views: 1352

Answers (2)

DVarga
DVarga

Reputation: 21829

what is the point of importing that class?

Imagine that you do not import classes inside java.util. If you want to create a Map you type:

java.util.Map<String, Integer> myMap = new java.util.HashMap<>();

If you import the class in that package like import java.util.*;:

Map<String, Integer> myMap = new HashMap<>();

If you are importing a class, and you don't have access to the members of that class

Imports do nothing with access, they are all about readability and convenience. You don't have to type that much and the second declaration is much more readable, but you can use the same methods of myMap in both cases.


The static import that the book mentions (from the doc):

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

A good example for static import is the usage of Mockito in unit tests. Without any imports you can verify some behavior like:

org.mockito.Mockito.verify(mock, org.mockito.Mockito.never()).someMethod();

If you use normal import import org.mockito.Mockito;:

Mockito.verify(mock, Mockito.never()).someMethod();

and with static import import static org.mockito.Mockito.*; you can type

verify(mock, never()).someMethod();

Here the verify and never static methods can be used even without specifying their class (Mockito).

Upvotes: 2

ajb
ajb

Reputation: 31699

If you are importing a class, and you don't have access to the members of that class, then what is the point of importing that class?

Suppose you have a package com.ebony.maw.utils, and that package has a class MyUtilities, and the class has a static method findFizgigs(). If you say

import com.ebony.maw.utils.*;

you can now say

MyUtilities.findFizgigs("thingy-002");

instead of having to say

com.ebony.maw.utils.MyUtilities.findFizgigs("thingy-002");

But you still can't say

findFizgigs("thingy-002");

without the class name. That's what they mean by importing the class, but not importing the method. It just means you can use the class name without having to supply a package name prefix. But you can still use the method--you just have to supply the class name as a prefix.

Upvotes: 2

Related Questions