Importing Classes


Premium Content - Free Preview

In the next node, we'll see how to get input using a Java Class called Scanner. Before we can use the Class however, we need to import it.

import

If you had a class called Blob in your project, you could use it right away without importing it. However, the Scanner isn't in your project, so you need to tell Java where to find it.

We saw previously that you could import ArrayList with the following line of code at the top of your code :

import java.util.ArrayList;

Similarly, you can import Scanner with this code:

import java.util.Scanner;

The import statement consists of 3 parts: the import keyword, the Package name, and the Class name.

import-description

package

A package is a collection of related Java classes. The standard Java library is organized into many different packages. To import a Class, you need to specify the package that it belongs to so that Java can find it. There could be identically-named classes in different packages, but packages each have their own unique name. java.util is an important package in the standard library that contains a large number of useful classes.


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]