Java Classes and Interfaces
Packages are named containers for related Java classes and interfaces.
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?
Reveal 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.
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.
What a Package Contains
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.
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.
- The package line names the container that holds Circle.
- The class declaration follows the package line.
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?
- 1Find the package line
The first line declares a named package, com.ijktec.app.
- 2Identify the class's home
The package declaration identifies the package of the current class, so Main belongs to com.ijktec.app.
- 3Consider removing the line
If the package declaration is omitted, Java places Main in the default package instead of com.ijktec.app.
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.
- 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.
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.
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 likeA file starts directly with public class Greeter and has no package line.
Why it failsOmitting a package declaration places the class in the default package, so there is no named package to point to.
Do this insteadAdd 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 likeWriting package com.ijktec.shapes; in the app file and expecting Circle to become usable there.
Why it failsA package declaration identifies the current class's own package. An import directive is what enables use of a class from another package.
Do this insteadKeep 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 likeAdding import java.lang.String; to a file that uses String.
Why it failsClasses in
java.langare automatically imported into every Java file, so the import is not needed for them.Do this insteadUse 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
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.langare 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.