Packages in Java

Packages in Java is a group of similar types of classes, interfaces and sub-packages. Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.

It is classified into two types, built-in package and user-defined package. Normally, there are many built-in packages in java and they are java, lang, awt, javax, swing, net, util, io, sql, etc..

In this post, we will have the detailed learning of creating and using our own Java packages.

Advantage of Java Package

  1. Java Package provides the access protection.
  2. Java Package is easy to maintain by categorizing the classes and interfaces.
  3. Java packages makes searching/locating and usage of classes, interfaces, enumerations and annotations easier.
  4. Java package removes naming collision. There can be two classes with the same name. For example: office.worker.manager.Staff and office.worker.ceo.Staff are the two classes with same name “Staff” in two packages.

Advantages of Creating Own Package in Java

It helps you to save lot of your time & it is a way of implementing your own packages. You don’t want to write functionality every time, instead you can just call the functions under your own package by passing the value.

 

How to Create Own Java Package? | Complete Step by Step Text Guide

Is it possible to Create Our Own Packages in JAVA?

Yes, you can even create your own package as you wish. Here i have explained how to create a package & how to import predefined function inside the existing packages. Follow the steps as mentioned below,

  • Open Notepad/Notepad++ to write the function.
  • Copy & Paste the code mentioned below

package java.util;

public class Gowdham {

public void printing() {

System.out.println(“The function printing is called”);

}

}

  • After pasting the above code, just save it (file -> save as) under the name you want with the extension .java. For example: If the file name is example then save it as example.java

In the above steps we have create a sample files & save it. Now we want to compile the java file. Follow the steps mentioned below to compile a java program in Windows Operating System.

  • Open Command Prompt
  • Navigate to the drive where you saved the java file. If you are saving the file in ‘G’ drive, then you can type ‘g:‘ and press enter key.
  • Now type javac filename.java (Replace the filename in the command with your filename)
  • Once you enter the above command, class file will be generated on the same location.
  • Open file explorer and check there will be a .class file under the same location.

Now we want to move this filename.class file into regular java file location.

Navigate to the folder as “Drive c -> Program Files -> Java -> jre1.8.0_111 -> lib”. Find “rt.jar” file. Copy and store it on different location for a backup.

Open the rt.jar (Executable Jar File) with WinRAR archiver. Navigate to java -> util. Now upload the class file that you compiled recently.

Now you can use some Java IDE & try executing the program.

 

How to Create Own Java Package? | Complete Step by Step Video Guide

If the above text guide looks complicated or you want to check how it actually works, you can check out our video guide. In this video guide, you can see the steps performed to create own package in Java and how it works.

You can implement your own functions using this method to do tricky executions within few lines of code. If you are creating own java packages then it will work only on your personal machine so people who see your coding will feel crazy about it.

If you are about to attend Java Interview or looking for a Java Examination, then you should check the Java Important Questions for better results.