Loading, please wait...

A to Z Full Forms and Acronyms

What is Java Package?

Jun 06, 2020 Java, Java basics, Java package, 5926 Views
Brief of Package

Packages in Java

What is the Java package?

Java package is a process of combing a group of classes, other interfaces, and sub-package in one application or a program. It consists of a familiar type of class and sub-packages.

Why we use packages?

There are multiple reasons that we should use the package in our application. Few reasons are:

  • Reusable: For avoiding naming conflict. For example, two different classes can import two different packages with the same but with a different code block.
  • Security: Easier to tag with access controls. This helps in the security of data.
  • Maintainability: Searching and accessing of the classes, interfaces, sub-packages, and other packages become easier.
  • Time-Saving: It saves a lot of time. We don’t need to write code again and again.
  • Saves Memory: It helps in keeping the memory safe. 

Types of Java Package

There are two types of Java Packages:

  1. Built-in Package
  2. User-defined Package

Built-in Package

It is a pre-defined package in Java. These packages have already existed. There is a long list of built-in packages such as swing, AWT, util. lang, SQL, etc.

Syntax:

The keyword which is used to include the package in a class is known as an import.

  • If you want to include only a package then,

import package_name.*; // * is used for including the whole package.

  • If you want to include only a specified class the,

import package_name.class_name; // class_name of that class which you want to include in a program.

Example:

import java.util.Scanner;

class MyClass 
{
     public static void main(String[] args) 
     {
      Scanner s= new Scanner(System.in);
      System.out.println("Enter username");
      String userName = s.nextLine();
      System.out.println("Username is: " + userName);
     }
}

In the above example, if a programmer wants to include or import the sub-packages, interfaces, and classes of the util package then just put (*) instead of scanner which is the name of the class in util package.

User-defined Package

It is defined by the user. It used in an application where we need to write a certain step of code again and again. If we create a package of specified code, then we only need to import that package, whenever it is in use. It saves the large unit of memory and also the amount of time of the programmer.

Syntax:

The keyword which is used to create a package is the package.

package package_name;

class class_name
{
     Public static void main(String args[])
     {
       System.out.println(“This is my package”);
     }
}

How to compile and run a package?

Compile javac -d . class_name_java

-d is used to specify the direction and . (dot) is used if we want to keep the package at the same location.

Runjava package_name.class_name

Example:

package newpack;
 
class demo
{
   Public static void main(String args[])
   {
    System.out.println(“This is my new package with demo class”);
   }
}

Compile: javac -d . demo_java

Run: java newpack.demo

The above example is just a sample of creating a package, we can create multiple packages according to our needs in an application. One application can consist of n number of packages depends on the requirement.

A to Z Full Forms and Acronyms

Related Article