Concepts / Classes and Objects in Java

Classes and Objects in Java

A package can contain both classes and other packages, and a package contained in another package is called a subpackage.

  • Programming

Why Package Names Look Longer Than They Seem

Java's standard library is organized into packages, and the names of those packages often look longer than they first appear. The package that provides the Scanner class, for example, is not called util on its own. To read a package name correctly, you need to understand how one package can sit inside another. This article traces package names from the outside in, so that by the end you can read a name such as java.io and say exactly where it lives.

Before reading on, try this quick check. Suppose you see the name java.util and someone asks you which package is the outer one. Decide your answer before reading the next section, then compare it with the walkthrough that follows.

Tracing a Name From Left to Right

Read a full package name from left to right, and each dot marks one step further into the structure. Take java.util. The leftmost part, java, is the outer package. The part after the dot, util, is a package that sits inside it. Reading the name from the inside outward would lose the relationship, so keep the direction in mind: the name records the chain of containers from the outermost one down to the one you are looking at.

javaouter containing package.one step deeper into thestructureutilsubpackage inside java
In the name java.util, which segment is the outer package and which is the subpackage it contains?

The diagram above shows the three visible parts of the name. The java segment is the container, the dot marks the step inward, and util is the thing held inside. Reading java.util therefore tells you that util is contained within java.

Comparing java.io and java.net

Now apply the same reading to java.io and java.net. Each has java on the left and a single additional segment on the right. In every case, the segment after the dot names a package that lives directly inside java. None of these three names needs more than one dot, because each sits exactly one level below java in the structure.

Worked example

Identifying the containing package of java.net

Given the full package name java.net, identify the subpackage and its containing package.

  1. 1
    Find the leftmost segment

    The leftmost segment is java, which is the outer package.

  2. 2
    Find the segment after the dot

    The segment after the dot is net, which names a package that sits inside java.

  3. 3
    Assign the roles

    net is the subpackage, and java is its containing package.

Result

net is a subpackage of java, and the containing package is java.

A Package Can Hold Classes and Packages

Definition

A package is a container. A package can contain both classes and other packages. When a package contains another package, the inner one is called a subpackage, and the package that holds it is the containing package.

This is the central fact of this section. The contents of a package are not limited to classes. A single package may hold ordinary classes at its own level while also holding other packages inside it. The terminology stays simple: the container is the containing package, and the thing inside it is the subpackage.

The source gives two top-level packages as examples of this structure. The java package and the javax package both contain subpackages. The java package has many of them, and the source names java.util, java.io, and java.net as examples. It notes that java.util is a subpackage of java. Because the same containment rule applies at every level, a subpackage can itself contain other packages, although the source's examples stay at the level directly below java.

javatop-level packagejava.utilutility subpackageScannerclass provided by java.utiljava.ioinput and output subpackagejava.netnetwork subpackagejava.langmost basic subpackagecontainscontainscontainscontainscontains
Which items sit inside java.util, and how do java.util, java.io, java.net and java.lang sit at the same level beneath java?

Read the diagram from top to bottom and it matches the naming rule from earlier. Each child box carries its parent's name followed by a dot. The java.util box holds utility classes, and the source connects it to the Scanner class discussed earlier in the textbook. The java.io box provides facilities for input and output, and the java.net box deals with network communication. The diagram shows only what the source describes. The source introduces its list of subpackages with the words such as, so java contains more subpackages than the four shown here.

Reconstructing a Name From the Hierarchy

Once you understand the containment chain, you can build a full package name from the position of a package in the hierarchy. Start at the outermost package and append each inner package, separated by dots, until you reach the one you want. This worked example extends the pattern of java.io and java.net one level deeper.

javalevel 1 outer packageiolevel 2 package inside javasublevel 3 inventedillustration
If a package sits at level 3 of the hierarchy, which dot-separated full name does it get, and which segment comes from each ancestor? The name sub is an invented illustration, not a real Java package.
Worked example

Reconstructing the full name of a level 3 package

A package named sub sits inside io, and io sits inside java. What is the full package name of sub?

  1. 1
    Start with the outermost package

    The outermost package is java, so the name begins with java.

  2. 2
    Append the next level

    io sits directly inside java, so append a dot and io to get java.io.

  3. 3
    Append the innermost level

    sub sits directly inside io, so append a dot and sub to get java.io.sub.

Result

The full name is java.io.sub. The package sub is an invented illustration of the rule and is not a real Java package.

java.lang as the Foundation Package

The source calls java.lang the most basic package. It contains fundamental classes, and the source lists four of them: String, Math, Integer, and Double. The source singles out this package because many other parts of Java programming rely on these kinds of classes.

The name java.lang still follows the containment pattern. It is written with java on the left, so it is one of the packages held inside java, alongside java.util, java.io, and java.net. Being the most basic package does not change how its name is built; it only describes what the package contains.

  • String: a fundamental class in java.lang
  • Math: a fundamental class in java.lang
  • Integer: a fundamental class in java.lang
  • Double: a fundamental class in java.lang

Mistakes When Reading Package Names

Several errors come up often when people first read package names. Each one usually comes from treating a package name as a flat label rather than as a path through containers.

  • Common mistake

    Reading the name as a single unrelated word

    What it looks like

    Treating java.util as if util were a standalone package.

    Why it fails

    The dot records containment. util sits inside java, so the name carries the chain of containers.

    Do this instead

    Read the name left to right: java is the outer package and util is inside it.

  • Common mistake

    Reading the name from the inside outward

    What it looks like

    Starting from util and working back to java as if util were the top level.

    Why it fails

    This loses the direction of the relationship, which runs from the outermost container down to the package you are looking at.

    Do this instead

    Keep the direction in mind: the name lists containers from outermost to innermost.

  • Common mistake

    Assuming a package can hold only classes

    What it looks like

    Believing that java.util can contain only classes and never another package.

    Why it fails

    A package can contain both classes and other packages.

    Do this instead

    Check what a package contains before deciding it is only a set of classes.

  • Common mistake

    Treating an illustrative name as a real package

    What it looks like

    Assuming java.io.sub exists in the standard library because the naming rule produced it.

    Why it fails

    The source does not describe a package called sub, so it is only a teaching example.

    Do this instead

    Apply the naming rule to reconstruct names, but verify that a package actually exists before using it.

Practice Reading Hierarchy Names

EASY

Using only the names discussed in this article, answer these three questions. First, in java.net, which segment is the subpackage and which is its containing package? Second, if a package named sub sits inside io, and io sits inside java, what is the full name of sub? Third, name two classes the source lists in java.lang and explain why the source treats java.lang as the most basic package.

Hints
  • Read the name from left to right, one dot at a time.
  • Start from the outermost package and append each inner package with a dot.
  • The source lists String, Math, Integer, and Double as classes in java.lang.

Key Takeaways

  • A package is a container, and it can hold both classes and other packages; a package held inside another is a subpackage.
  • Reading a full package name from left to right traces the chain of containers, so java.util means util sits inside java.
  • To reconstruct a package name, start at the outermost package and append each inner package with a dot.
  • java.lang is the most basic package and contains fundamental classes such as String, Math, Integer, and Double.