2.1. Introduction¶
As you transition from small programming projects to larger, real-world applications (sometimes with hundreds of Java files), it becomes increasingly important to organize the files in your project. Just as you may might organize your personal photos into separate folders, we organize code into packages.
Note
Java packages allow programmers to organize classes that have related functionality. Proper code organization becomes increasingly important as the size of the project increases.
In this chapter, you will see that organizing code is similar to
organizing other files on your computer, as we will need to place our
source code files (.java
files) into separate directories. However,
we will also need to slightly modify the source code and adjust our
compilation commands.
2.1.1. Existing Java Packages¶
You may not have realized it, but classes that you have used in the
past from the standard Java libraries are already organized into
packages. For example, the Scanner
class and the Math
class
are in two separate packages. The Scanner
class is included in the
utility package java.util
and the Math
class is part of the
fundamental language package java.lang
.
When a class is contained within a package, its fully qualified name (or FQN for short) consists of two parts: the name of the package followed by the class name (sometimes referred to as the simple class name to differentiate it from the FQN). By convention, the simple class name will always begin with a capital letter and the package name will be all lowercase.
Simple Class Name |
Package Name |
Fully Qualified Name |
Common Uses |
---|---|---|---|
|
|
|
Used to parse input from various sources. Typically used for reading user input. |
|
|
|
Provides common math functions such as |
Note
When you import a class, you have to use its FQN, but once the class is imported, you can use its simple class name.
You have to import classes from other packages in order to use them
in your programs. The only exception is that you don’t have to
import classes from java.lang
as those are automatically
included.
2.1.2. Primary Benefits¶
The two primary benefits of packages that we will see in this course are:
Name Space Management:
Packages allow you to give a common name to a group of related types. For example,
java.util.Scanner
andjava.util.Random
are two utility classes provided in thejava.util
package. You and other programmers can easily determine that these types are related based on their package. They are both utilities that you have probably used in your code.Note
You could have two types with the same simple name that are located in different packages. For example, you could have a
java.util.Random
class and aedu.cs.uga.Random
class. Both classes have the same simple class name (Random
) but they can be differentiated by their package names.Access Protection:
Visibility in Java is not limited to
public
andprivate
. Packages and additional visibility modifiers enable programmers to declare entities as visible only within a package. We will discuss this in more detail when we cover visibility.
Further Reading
For more information about packages in Java see [1]
In this chapter, you will create multiple classes, group them into a package, then compile and run them. The expectation is that you will follow along with this tutorial in a terminal emulator on Odin or some Unix machine. You should ask questions on Piazza if you are unable to proceed or if some aspect of the tutorial is particularly confusing.
Quick Review Questions
Given the FQN
java.util.ArrayList
, what is the simple class name and package name?If you want to import the
ArrayList
class, what is the exact line of code you would need to add to your program and where would that line need to be located?Given the FQN
javafx.scene.control.Button
, what is the simple class name and package name?
Quick Review Answers (Don’t open until you complete the questions above)
The simple class name is
ArrayList
and the package name isjava.util
.The exact line would need to be
import java.util.ArrayList;
and it would need to be at the top of the Java file.The simple class name is
Button
and the package name isjavafx.scene.control
.