Java Packages & API
A package in Java is used to group related classes. Think of it as a folder in a file directory. We
use packages to avoid name conflicts, and to write a better maintainable code. Packages are
divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
Built-in Packages
The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Environment.
The library contains components for managing input, database programming, and much much
more. The complete list can be found at Oracles
website: [Link]
The library is divided into packages and classes. Meaning you can either import a single class
(along with its methods and attributes), or a whole package that contain all the classes that
belong to the specified package.
To use a class or a package from the library, you need to use the import keyword:
SyntaxGet your own Java Server
import [Link]; // Import a single class
import [Link].*; // Import the whole package
Import a Class
If you find a class you want to use, for example, the Scanner class, which is used to get user
input, write the following code:
Example
import [Link];
In the example above, [Link] is a package, while Scanner is a class of the [Link] package.
To use the Scanner class, create an object of the class and use any of the available methods found
in the Scanner class documentation. In our example, we will use the nextLine() method, which is
used to read a complete line:
Example
Using the Scanner class to get user input:
import [Link];
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);
[Link]("Enter username");
String userName = [Link]();
[Link]("Username is: " + userName);
Import a Package
There are many packages to choose from. In the previous example, we used the Scanner class
from the [Link] package. This package also contains date and time facilities, random-number
generator and other utility classes.
To import a whole package, end the sentence with an asterisk sign (*). The following example
will import ALL the classes in the [Link] package:
Example
import [Link].*;
User-defined Packages
To create your own package, you need to understand that Java uses a file system directory to
store them. Just like folders on your computer:
Example
└── root
└── mypack
└── [Link]
To create a package, use the package keyword:
[Link]
package mypack;
class MyPackageClass {
public static void main(String[] args) {
[Link]("This is my package!");
Save the file as [Link], and compile it:
C:\Users\Your Name>javac [Link]
Then compile the package:
C:\Users\Your Name>javac -d . [Link]
This forces the compiler to create the "mypack" package.
The -d keyword specifies the destination for where to save the class file. You can use any
directory name, like c:/user (windows), or, if you want to keep the package within the same
directory, you can use the dot sign ".", like in the example above.
Note: The package name should be written in lower case to avoid conflict with class names.
When we compiled the package in the example above, a new folder was created, called
"mypack".
To run the [Link] file, write the following:
C:\Users\Your Name>java [Link]
The output will be:
This is my package!