Skip to content
Related Articles

Related Articles

User-Defined Packages in Java

Improve Article
Save Article
Like Article
  • Last Updated : 01 Dec, 2021

Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. In Java, it is used for making search/locating and usage of classes, interface, enumerations, and annotations easier. It can be considered as data encapsulation also. 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 package like*, java. lang.* etc., are known as built-in packages.
  • User-defined packages: As the name propose, 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.

User-defined Packages

User-defined packages are those packages that are designed or created by the developer to categorize classes and packages. They are much similar to the built-in that java offers. It can be imported into other classes and used the same 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.


package package-name;

Steps to create User-defined Packages

Step 1: Creating a package in java class. The format is very simple and easy. Just write a package by following its name.                                             

package example1;

Step 2: Include class in java package, But remember that class only have one package declaration.

package example1;

class gfg {
   public static void main(Strings[] args){

Step 3: Now the user-defined package is successfully created, we can import it into other packages and use 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, 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’ll create a user-defined package and function to print a message for the users.


// Java program to create a user-defined
// package and function to print
// a message for the users.
package example;
public class gfg {
    public void show()
        System.out.println("Hello geeks!! How are you?");
    public static void main(String args[])
        gfg obj = new gfg();;

Output 1:

Hello geeks!! How are you?

Example 2:

In the below-mentioned example, we’ll import the user-defined package “example” created in the above example. And use the function to print messages.


import example.gfg;
public class GFG {
    public static void main(String args[])
        gfg obj = new gfg();

Output 2:

Hello geeks!! How are you?

In the above examples, First, we created a user-defined package name “example” under that we have the class name “gfg,” which consist of a function shown to print greeting message. After compilation and execution of code, we’ll get the message “Hello geeks!! How are you?”. And in the next example, we imported the user-defined package name “example” and used the function show. So in the output of both programs, you can see that we have a greeting message.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!