Java Packages
To better serve the demands of the learners, the topic of Java packages is presented.
Java Packages & API
When we discuss Java packages, we mean that a package in Java is used to group classes that are related.
A folder in a file directory comes to mind. By using packages, we can reduce name conflicts and create more dependable code. There are two categories for packages:
- Integrated Packages (packages from the Java API).
- Customized Packages (create your own packages).
Built-in Packages
The Java Development Environment contains a library of prewritten classes called the Java API, which is free to use.
The library includes a huge variety of components, including ones for database development and input management. On Oracle’s website, at Java-api page, you can find the entire list.
There are classes and packages in the library.
When it comes to Java packages, this means that you may either import a single class (together with all of its methods and properties) or a whole package that contains all of the classes that are a part of the specified package.
You must use the import keyword to use a class or package from the library:
Syntax
import package.name.Class; // Only a single class is imported
import package.name.*; // The whole package is imported
Import a Class
Write the following code if you discover a class that you want to utilize, such as the Scanner class, which is used to collect user input.
Example
import java.util.Scanner;
When it comes to Java packages, Scanner is a class of the java.util package in the given example, whereas java.util is a package.
You can utilize any of the various methods listed in the Scanner class documentation by creating an object of the class and using it.
In this example, we’ll read an entire line using the nextLine() method.
Getting user input with the Scanner class:
Example: 
Example: 
Import a Package
There are many various packages available. We utilized the Scanner class from the java.util package in the previous example. Along with date and time capabilities, this package also includes a random-number generator and other utility classes.
Add an asterisk (*) at the end of the phrase to import the entire package. All of the classes in the java.util package will be imported in the example that follows:
Example: 
User-defined Packages
You must be aware that Java stores packages in a file system directory in order to construct your own. just like your computer’s folders:
Example
└── root
└── mypack
└── MyPackageClass.java
Use the keyword “package” to construct a package:
Example: 
The file should be saved as Custom_Package.java, then compiled.
C:UsersYour Name>javac Custom_Package.java
Compile the package after that:
C:UsersYour Name>javac -d .Custom_Package.java
This causes the creation of the “mypack” package by the compiler.
The class file’s destination is specified by the -d keyword. You can use any directory name, such as c:/user (Windows), or the dot sign “.”, like in the example above, to retain the package in the same directory.
Remember: To prevent name conflicts with classes, the package name should be put in lower case.
A new folder named “mypack” was produced when the example package in the previous section was constructed.
Write the following to start the Custom_Package.java file:
C:UsersYour Name>java mypack.Custom_PackageClass
The result will be
This is my first custom package!!