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:

  1. Integrated Packages (packages from the Java API).
  2. 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:


import; // Only a single class is imported
import*; // 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.


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:


import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner mrx_value=new Scanner(; System.out.println("Write the topic of discussion here: ");String mrx_name=mrx_value.nextLine(); System.out.println("Topic : "+mrx_name);} }


import java.util.Scanner; public class Main { public static void main(String[] args) {Scanner mrx_input=new Scanner(; System.out.println("Enter Number 1: "); int mrx=mrx_input.nextInt();System.out.println("Enter Number 2: "); int ample=mrx_input.nextInt();System.out.println("Sum of Integers is: "+(mrx+ample));} }

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:


import java.util.*; // Here we import the package named 'util'public class Main { public static void main(String[] args) {Scanner input=new Scanner(;System.out.println("Enter the name of the topic we are learning: "); String topic= input.nextLine();System.out.println("\n We are learning : "+topic);} // Output: We are learning : Java Packages and API }

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:


└── root
  └── mypack

Use the keyword “package” to construct a package:


package mypack; public class Custom_Package{ public static void main(String[] args) { System.out.println("This is my first custom package!!"); } // Output: This is my first custom package }

The file should be saved as, then compiled.

C:UsersYour Name>javac

Compile the package after that:

C:UsersYour Name>javac -d

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 file:

C:UsersYour Name>java mypack.Custom_PackageClass

The result will be

This is my first custom package!!

Another Example: 

package mypack;public class Custom_Package { public static void main(String[] args) { System.out.println("This is another example to create a custom package !!"); } // Output: This is another example to create a custom package !! }
We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *