Concepts / Java Classes and Interfaces

Java Classes and Interfaces

Packages are named containers for related Java classes and interfaces.

  • Programming

Why Every Class Has a Home

When you open a Java source file, the class inside it can look like a self-contained piece of code. In practice, every Java class belongs to a package, and that membership shapes how the class is named, found, and used by other code. This article follows a single question through the rest of the lesson: where does a class belong? Once you can answer that, you can tell whether another file needs an import directive to use the class.

What do you think happens?

A Java file contains a class named Greeter and has no package line at the top. Which package is Greeter in?

  • A named package called Greeter

  • The default package

  • The java.lang package

Reveal answer
Answer:

The default package

If a class is not explicitly placed into a named package, Java puts it in the default package. Omitting the package declaration is what causes this.

Asking Where a Class Belongs

When you look at a Java class, it helps to ask one question before asking what the class does: where does the class belong? Java places every class inside a package. A package is a collection of related classes and interfaces, so it gives Java types a named container.

The package is not an action performed on the class later. It is the class's stated organizational home. Because that home is declared as part of the class's source file, you can read it directly from the file, and you can use it to decide whether a different file needs to refer to the class in a special way.

Key point

Read the package first. The package tells you the class's home, and that home is what determines whether another file must import the class before using it.

Named Packages and the Default Package

There are two ways a class can end up in a package. In the first, the source file declares a named package, so the class is placed into that named container. In the second, the source file omits the package declaration, and Java places the class in the default package. The two cases differ only in the declaration, but the difference affects where the class lives and how other code refers to it.

The default package is convenient for very small beginner exercises, because no package name has to be introduced at the start of the source file. That convenience is the reason it appears so often in first programs. It is not the recommended home for real code.

Named packagepackagecom.ijktec.shapes;explicit package lineDefault packageNo package linedeclaration omitted
How a class in a named package and a class in the default package differ in where they live and how they are declared.

What a Package Contains

Definition

A package is a named container for related Java classes and interfaces. A package holds types that belong together, and its name identifies the container.

Java's standard classes are stored in named packages. String and System are both standard classes in the java.lang package. The name java.lang is a dotted name, and the dot separates the parts of the package name. The diagram below shows where these two standard classes live.

java.langStringSystemcontainscontains
Which named package holds the standard classes String and System?

Notice that a package holds classes and interfaces, not just classes. The definition covers both kinds of Java type. When you read a package name, you are reading the name of a group of related types, which is why the name is useful for finding them.

The Package Declaration Line

A package declaration identifies the package that the current class belongs to. It is written as a single line in the source file, and it sits at the start of the file, before the class itself. The line below is a generated example that places a class named Circle into the package com.ijktec.shapes.

java
Notes on the code
  • The package line names the container that holds Circle.
  • The class declaration follows the package line.
packagekeyword that starts thedeclarationcom.ijktec.shapesdotted name of the package;ends the declaration
Which parts of the package line name the package, and what does each token do?
Worked example

Working Out a Class's Package

Given a source file that begins with the line package com.ijktec.app; followed by public class Main, which package does Main belong to, and what would change if the first line were removed?

  1. 1
    Find the package line

    The first line declares a named package, com.ijktec.app.

  2. 2
    Identify the class's home

    The package declaration identifies the package of the current class, so Main belongs to com.ijktec.app.

  3. 3
    Consider removing the line

    If the package declaration is omitted, Java places Main in the default package instead of com.ijktec.app.

Result

Main belongs to com.ijktec.app. Removing the package line would move it into the default package, which style guidelines discourage.

Importing a Class From Another Package

The package declaration says where the current class lives. An import directive does a different job: it enables the current file to use a class that lives in another package. When Main in package com.ijktec.app needs Circle from package com.ijktec.shapes, the file contains an import directive naming Circle. The example below is generated for this article.

Notes on the code
  • The package line places Main in com.ijktec.app.
  • The import directive brings Circle from com.ijktec.shapes into this file.
  • Circle is then used by its simple name inside main.
Before
package com.ijktec.app;

public class Main {
    public static void main(String[] args) {
        Circle c = new Circle(2.0);
    }
}

Circle has not been imported into Main's file, so the simple name Circle does not refer to the class in com.ijktec.shapes.

After
package com.ijktec.app;

import com.ijktec.shapes.Circle;

public class Main {
    public static void main(String[] args) {
        Circle c = new Circle(2.0);
    }
}

The import directive names Circle, so the simple name refers to com.ijktec.shapes.Circle inside this file.

Mistakes With Packages and Imports

  • Common mistake

    Assuming every class has an explicit package line

    What it looks like

    A file starts directly with public class Greeter and has no package line.

    Why it fails

    Omitting a package declaration places the class in the default package, so there is no named package to point to.

    Do this instead

    Add a package line such as package com.ijktec.greetings; to place the class in a named package.

  • Common mistake

    Treating the package declaration as an import

    What it looks like

    Writing package com.ijktec.shapes; in the app file and expecting Circle to become usable there.

    Why it fails

    A package declaration identifies the current class's own package. An import directive is what enables use of a class from another package.

    Do this instead

    Keep the package line for the file's own class and add import com.ijktec.shapes.Circle; to use Circle.

  • Common mistake

    Importing java.lang classes unnecessarily

    What it looks like

    Adding import java.lang.String; to a file that uses String.

    Why it fails

    Classes in java.lang are automatically imported into every Java file, so the import is not needed for them.

    Do this instead

    Use String and System directly without an import from java.lang.

  • Common mistake

    Placing a real project class in the default package, which style guidelines discourage

Checking Your Understanding

MEDIUM

A file begins with package com.ijktec.report; and contains class Summary, which needs to use a class named Chart that lives in package com.ijktec.charts. Say which package Summary belongs to, what line must be added so Summary can use Chart, and whether String needs an import in the same file.

Hints
  • The package declaration tells you the class's own home.
  • An import directive is how a file uses a class from another package.
  • Classes in java.lang are imported automatically.

Key Takeaways

  • Java places every class inside a package, which is a named container for related classes and interfaces.
  • A class declared with a package line belongs to that named package. A class with no package declaration belongs to the default package, which style guidelines discourage.
  • The package declaration identifies the current class's package. An import directive enables a file to use a class from another package.
  • Classes in java.lang, such as String and System, are imported automatically into every Java file.