Related Java Classes can be grouped together in something called a package. A lot of people like to think of this as being like a folder in your file system, where you group related files. The difference is, that it is your related Java Classes.
We’ve already looked at UML packages, and Java packages are very similar. Where a UML package is often more logical in nature, Java packages are both logical, and physical as you’ll see.
Let’s say you worked for a company that created different types of games. You might have several packages that you create so you can share related classes that are necessary to work between them. For example, you might have a utilities package that included timers, score keepers, players, virtual dice, etc. You might have another package that was for terrain maps, which had not only a general map class, but also classes for different types of terrain such as woods, rivers, coast, etc.
When you’d go to create a game, you might always include the utilities package, no matter the game, but not necessarily the terrain package if you didn’t need it.
There are generally two types of packages,
- Built in Java packages – usually starting with
java.
orjavax.
- User defined packages – where you can create them, someone else within your organization, or an external 3rd party
Some other languages have the same concept as a package, but they may call it a library.
Importing a Package
To import a package, you use the import
keyword and specify the package name. Typically a package name, if it’s not part of the same project, looks like reverse domain name, starting with .com for those coming from a business or .org for non-profit and open source projects.
Then you have the package name, which may be further refined into sub packages, just like you would have with sub-folders.
You can either import the entire package, or a specific class. If you want to import an entire package, you add a .*
to the end of the package. If you want a specific class, you specify .<class name>
. See the examples below.
import java.util.Date; // importing a specific class from the package
import java.util.*; // importing the whole package
import com.megacorp.games.*; // importing a sub package
Note, that if you import a package, and it has a sub-package, you do not get the sub-package. You would then need to import that on another import line;
While it is easy to import packages, the java.lang
package is already imported for you by default. If you want to know about the packages that come with Java, you can look at the API list of packages and classes.
Adding a Class to a Package
The package itself is like a zip file of classes. However, you cannot add items to an existing package if you did not own/create it. Therefore we’ll look at packages that are user defined.
Since Java is looking at the package like a folder structure, it is important to create a folder structure on your computer to get it work correctly.
└── root └── mypackage └── MyPackageClass.java
Then you add the package name in your .java file like you see below:
package mypackage;
public class SampleClass1 {
// ....
}
Notice that the package keyword and name are both before you define the class. The package name also needs to meet the folder which the file is stored in.
As a manner of practice, package names and the folder name for the package, are all lowercase to avoid potential conflicts with class names.
Java Packages was originally found on Access 2 Learn