Importing Packages
Estimated time to read: 1 minute
Overview¶
In order to use members from a package that is not that package we are currently writing code for, they must be imported.
Package Members¶
Only public package members are accessible outside the package in which they are defined.
To use a public package member from outside its package, one must either: - Refer to the member by the fully qualified class / interface name - Import the package member - Import the entire package the member belongs to
import package1[.package2][.package3].className;
Import statement¶
A class may access the contents of a java package through the use of an import statement.
package io.entityfour.simple;
import java.io.*; //Import all classes from java.io
import java.sql.Connection; // Import Connection only from java.sql
public class DatabaseReader {
Connection conn = new Connection();
}
Specifying an import statement¶
- A give file may include zero or more import statements.
- An
import
statement is placed between the package declaration and the beginning of the class itself. - A single class is imported by using its fully qualified package name, example below:
Wildcards¶
The contents of an entire package my be made accessible to your class by using a wildcard import statement.
Using a wildcard provides access to call classes within the specified package.