Last Updated: 21 September, 2023
A package is a collection of similar types of classes, interfaces, enums, sub-packages, etc., which helps in preventing naming conflicts, providing access protection and better organization, etc.
Java provides some built-in packages like java, lang, util, io, etc., but we can also create our own (user-defined) packages similar to built-in packages. If we have a large number of classes, interfaces, etc., it will be easier to manage if we classify them according to their functions.
📝 The package keyword is used to create a package in Java.
Java supports only two types of packages:
In the built-in packages, Java provides a large number of prewritten classes, interfaces, etc. grouped into different packages based on their functionality, and they are also known as Java API packages.
Please see the built-in packages in Java as given below.
Some of the commonly used built-in packages are:
java.lang | This is the default package of Java, and all the classes are automatically imported; there is no need to explicitly import them. The lang package provides all the basic required classes, interfaces, etc. for developing Java applications. |
java.util | This package contains the Collections Framework, Date and Time facilities, Internationalization, and miscellaneous utility classes and interfaces. |
java.io | The java.io package provides classes and interfaces, etc., for system input and output through data streams, serialization, and the file system. |
java.applet | This package provides classes, interfaces, etc. for creating applets in Java. |
java.awt | This package provides classes, interfaces, etc. for developing GUI (Graphical User Interface) based Java applications. |
java.net | Contain classes for supporting networking operations. |
User-defined packages are developed by users. Same as build-in packages, users can also create packages, sub-packages, classes, etc. Now we will see how to create and manage user-defined packages.
Creating a package in Java is a very simple process. To create a package, we need to use the package keyword. The package statement must be the first line (except blank lines and comments) in the Java source code, and it must be declared only once.
While creating a user-defined package, we must follow the naming convention and defined rules as given below.
Package names should be dot-separated, but do not end the package name with a dot (.). For example,
✅ package com.javabytechie.admin;
❌ package com.javabytechie.admin.;
We cannot use any Java reserved words like abstract, true, false, for, while, primitive data types, etc. as a package name. For example,
✅ package com.javabytechie.admin;
❌ package com.javabytechie.super.admin;
As per the recommended standard, the reverse domain naming convention is to be followed when declaring a user-defined package.
Syntax: package org.company.project.module.submodule;
Example: package org.javabytechie.javabank.payment.online;
Let's understand the user-defined package with the help of the given example below.
Implementation of a user-defined package
Access the above created class in a different package.
Another way of importing a class from a different package.
Instead of an import statement, we can also write a full classpath name, but it is not a recommended way. We follow this way only if the same class name is already imported from a different package in the same class.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the Java default package?
java.lang is the default package in Java. The JVM internally loads this package.
Does importing a package import the sub-packages as well?
No, one will have to explicitly import the sub-packages.
What is the difference between import and static import statements?
The import features allows access to classes of a package without package qualification, whereas the static import feature (added in Java 5) allows accessing the static members of a class without class qualification. The import provides accessibility to classes and interfaces, whereas the static import provides accessibility to static members of the class.
What are the advantages of the Java package?
Java packages help to resolve naming conflicts when different packages have classes with the same names. This also helps us organise files within our project. For example, the java.io package does something related to I/O. If we tend to put all.java files into a single package as the project gets bigger, then it would become a nightmare to manage all our files.
Can we import the same package or class twice? Will the JVM load the package twice at runtime?
One can import the same package or the same class multiple times. Neither the compiler nor the JVM complain about it. And the JVM will internally load the class only once, no matter how many times you import the same class.
What is the first keyword used in a Java class?
The ‘package’ is the first keyword used in a Java class.
What is the naming convention to be followed for declaring a user-defined package in Java?
Ans. The reverse domain naming convention is to be followed for declaring a user-defined package in Java, as given below.
com.companyname.projectname.module.submodule;