User-Defined Packages in Java
Last Updated :
02 Jun, 2025
Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. In Java, it is used for making the search/locating and usage of classes, interfaces, enumerations, and annotations easier. It can also be considered data encapsulation. In other words, we can say a package is a container of a group of related classes where some of the classes are accessible are exposed, and others are kept for internal purposes.
Types of Packages
Packages are categorized into two parts. These are:
- Built-in packages: The already defined packages like java.io.*, java. lang.* etc., are known as built-in packages.
- User-defined packages: As the name suggests, user-defined packages in Java are essentially packages that are defined by the programmer. Whenever we want to add a class to the package, we have to mention the package name and the “package” keyword at the top of the program.
Now, we are going to learn more about user-defined packages in Java.
User-defined Packages
User-defined packages are those packages that are designed or created by the developer to categorize classes and packages. They are very similar to the built-in that Java offers. It can be imported into other classes and used in the same way as we use built-in packages. But if we omit the package statement, the class names are put into the default package, which has no name.
To create a package, we're supposed to use the package keyword.
Syntax:
package package-name;
Steps to Create User-defined Packages
Step 1: Creating a package in a Java class. The format is very simple and easy. Just write a package by following its name.
package example1;
Step 2: Include the class in a Java package, but remember that a class only has one package declaration.
package example1;
class Geeks {
public static void main(Strings[] args){
-------function--------
}
}
Step 3: Now the user-defined package is successfully created, we can import it into other packages and use its functions it.
Note: If we do not write any class in the package, it will be placed in the current default package.
In the below-mentioned examples, in the first example we'll create a user-defined package name "example" under that we've class named "gfg," which has a function to print message. In the second example, we will import our user-defined package name "example" and use a function present in that package.
Example 1: In this example, we will create a user-defined package and function to print a message for the users.
Java
// Java program to create a user-defined
// package and function to print
// a message for the users
package example;
// Class
public class Geeks {
public void show()
{
System.out.println("Hello geeks!! How are you?");
}
public static void main(String args[])
{
Geeks obj = new Geeks();
obj.show();
}
}
Output:
Hello geeks!! How are you?
Example 2:
In the below-mentioned example, we will import the user-defined package "example" created in the above example. And use the function to print messages.
Java
import example.Geeks;
public class Test {
public static void main(String args[])
{
Geeks obj = new Geeks();
obj.show();
}
}
Output:
Hello geeks!! How are you?
Explanation: In the above examples, first, we created a user-defined package name "example" under that we have the class name "Geeks," which consists of a function show() to print greeting messages. After compilation and execution of code, we will get the message "Hello geeks!! How are you?". And in the next example, we imported the user-defined package name "example" and re-used the function show() from the Geeks class. So in the output of both programs, you can see that we have a greeting message.
Similar Reads
Built-in Packages in Java In Java, Packages are used to avoid naming conflicts and to control the access of classes, interfaces, sub-classes, etc. A package can be defined as a group of similar types of classes, sub-classes, interfaces, or enumerations, etc. Using packages makes it easier to locate or find the related classe
7 min read
Java.util Package in Java Java.util PackageIt contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).Following are the Important Classes in Java.util package
4 min read
Java.io Package in Java Java.io Package in JavaThis package provides for system input and output through data streams, serialization and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. Follo
1 min read
Java Packages Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and interfaces. Packages are used for: Prevent naming conflicts by allowing classes with the same name to exist in different packages, like college.staff.cse.Employee and college.staff.ee.Employee.They make it easie
8 min read
JEP Package Tool in Java J package tool was introduced as an incubation tool in java 14. It remained an incubation tool till java 15. Â It packages java applications into platform-specific features. Basically, this tool converts our java source file into an executable file. In Windows, the executable file is of two types .ex
3 min read