Concepts / Import Statements in Java

Import Statements in Java

A package groups related classes, and it can also contain other packages, which gives Java a way to organize code at several levels.

  • Programming

A Library Too Large to Scan

Java ships with a very large collection of ready-made classes. Each one offers tools for a specific job, such as reading user input, doing arithmetic, or communicating over a network. If every one of those classes sat in a single undivided list, finding the right one would be slow and confusing. Java solves this problem with packages, which give the library a shape that a programmer can navigate.

Keep one question in mind as you read: when you see a name such as java.util, what does each part of that name tell you about where a class lives? By the end of this article you should be able to answer that question for any standard package you meet.

Packages as Labeled Containers

Think of a package as a labeled container that holds related material. The container's job is to keep things that belong together in one place. Inside a package you will find classes, and a package may also hold other packages. That second possibility is what lets Java organize code at more than one level of detail.

The analogy has a limit worth noticing. In Java, a package is a grouping of names and classes, and it does not itself perform any work. Its value is organizational: it tells you which classes belong together and gives each class a complete name that is unambiguous.

Key point

A package groups related classes. It never runs code on its own. Its purpose is to answer two questions: which classes belong together, and what is the complete name of each class.

Packages That Contain Other Packages

The java package is the clearest case of nesting. It holds several subpackages of its own. One of them is util, which contains a variety of utility classes, including the Scanner class that you have probably already used to read input. Because util sits inside java, its name is not just util. Its full name is java.util, with the parent name written first and a dot separating each level.

The diagram shows the nesting as a family tree. The subpackages io and net also live inside java. They are siblings of util, not children of it. That is why their full names are java.io and java.net rather than java.util.io or anything similar. The diagram shows only the parts the source describes; other classes and subpackages exist, but they are outside this material.

Definition

A subpackage is a package contained in another package. Its full name includes every parent package, so java.util is a subpackage of java.

Assembling a Full Name Segment by Segment

The procedure for names runs in two directions. When you build a name, you start from the outermost container and add each inner level in order. When you take a name apart, you read from the outside in. Each dot in a full name marks a boundary between a container and something it contains. The last segment is the class, and everything before it is the chain of packages that holds it.

Worked example

Building the full name of the io subpackage

The io subpackage is a child of java. Write its full name.

  1. 1
    Identify the parent

    io sits inside java, so java is the parent package and comes first in the full name.

  2. 2
    Add the subpackage name

    Place a dot after the parent name, then write the subpackage name io.

  3. 3
    Check the result

    The full name is java.io. It is not util.io, because io is a sibling of util inside java, not a child of it.

Result

java.io

What do you think happens?

Since java.util is the full name of util, what do you think the full name of the io subpackage is?

  • java.util.io

  • java.io

  • io.java

Reveal answer
Answer:

java.io

io is a subpackage of java, and it is a sibling of util. Its full name is built from its parent java, so it is java.io, not java.util.io.

The java, javax, and java.lang Packages

The entire standard Java API is spread across a few top-level packages. The package named java is the largest of the group. It contains several non-GUI packages, along with the original AWT graphical user interface classes. The package named javax also appears in the standard library. It holds the classes used by the Swing graphical user interface, along with many other classes. Both java and javax contain subpackages.

Within java, the most basic package is java.lang. It contains fundamental classes such as String, Math, Integer, and Double. Because these are the classes Java programs use most often, you rarely need an import statement for them.

Where an Import Statement Points

An import statement tells the compiler which package a class lives in, so the class can be used by its short name. The import line uses the full name you have been learning to build. The example below uses java.util.Scanner, a class from the java.util subpackage described in the source.

java
Notes on the code
  • The import line gives the full name java.util.Scanner, built from parent package java, then subpackage util, then class Scanner.
  • Inside the class, Scanner is used by its short name because the import line has already named its package.

Mistakes When Writing Package Names

  • Common mistake

    Writing the subpackage name alone

    What it looks like

    util instead of java.util

    Why it fails

    util is a subpackage inside java, so its full name must include its parent.

    Do this instead

    Write the full name java.util.

  • Common mistake

    Nesting a sibling under the wrong parent

    What it looks like
    java.util.io
    Why it fails

    io is a sibling of util, not a child of it. Both live directly inside java.

    Do this instead
    Write java.io.
  • Common mistake

    Using the wrong separator

    What it looks like

    java/util

    Why it fails

    The source specifies that a dot separates each level of a full name.

    Do this instead
    Write java.util.
  • Common mistake

    Changing the capitalization of the top-level package

    What it looks like

    Java.util

    Why it fails

    The source names the top-level package as java in lowercase, so an uppercase J does not match the name the source gives.

    Do this instead
    Write java.util.

Reading Package Names Yourself

EASY

Without looking back, take the full name java.util.Scanner apart. Name the top-level package, the subpackage, and the class. Then say which package holds java.lang and what kind of classes it contains.

Hints
  • Read from the outside in: the first segment is the top-level package.
  • The last segment after the final dot is the class.
  • java.lang holds fundamental classes such as String, Math, Integer, and Double.

Key Takeaways

  • A package groups related classes, and it can also contain other packages, so Java organizes code at several levels.
  • A subpackage's full name includes every parent package, with a dot separating each level, such as java.util.
  • The java package holds fundamental and non-GUI packages, javax holds Swing GUI classes and more, and java.lang holds classes such as String, Math, Integer, and Double.
  • Sibling subpackages such as java.io and java.util are both contained in java, and a correct full name reflects that structure.