Open In App

User-Defined Packages in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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, interfaces, enumerations, and annotations easier. It can be considered 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.io.*, 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.

Syntax:

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 has one package declaration.

package example1;

class gfg {
   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’ll 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 gfg {
 
    public void show()
    {
        System.out.println("Hello geeks!! How are you?");
    }
 
    public static void main(String args[])
    {
        gfg obj = new gfg();
        obj.show();
    }
}


Output:

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.

Java




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


Output:

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 consists of a function shown to print greeting messages. 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.



Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads