Package in java

The package in java is one of the most important features of Java. If you are learning Java, you need to understand the packages in java. Here we will describe what is the package in java, steps to create the package in java with the example, java package naming convention, default package in java, user-defined packages in java.

Here is the table content of the article will we will cover this topic.
1. What is the package in Java?
2. Build-in package?
3. User-defined package?
4. How the package is working internally?

5. Important points about the package?
6. A real-life example of the package?
7. How to create a package?

i) Creating package in java (Without IDE)
 ii) Creating package with IDE(Eclipse)
8. java package naming convention
9. Adding a Class in the existing package
10. Adding a class in the package while importing another package
11. Advantages of package

12. Access of package

What is the package in Java?

On your computer, you must saw the different drives, folders, and subfolders that use classify the various files. We can provide the accessibility of various files and folders. Like folders in the computer, we use packages in java and manage the Java applications.  The main use of the package is to organize the classes, interfaces, enum, and sub-packages.

Package in java is a group of similar types of classes, interfaces, enumerations, and sub-interfaces. A package is like a container that can have a group of related classes. Where some of the classes are accessible and some of them kept for internal purposes. It is used to organize the classes, interfaces and also provide access protection and namespace management

Suppose you are creating an application that will contain 100 classes and interfaces. So, it would be better if we use the package and organize your classes into a folder structure and make it easy to locate and use them. It will help to improve re-usability.

In java, there are many in build packages like java.lang, java.util, java.io, and java.net. All the packages contain the number of classes, interfaces, enums, and sub-packages.  All these packages are defined as a very clear and systematic packaging mechanism for categorizing and managing.

Let’s say you want to print the current date in java. Here you need to invoke the constructor of the Date() class. The Date class exists in java.util package. So we need to import that package into our class.

import java.util.Date;
public class EncapsulationExample
{
    public static void main(String args[])
    {
    	System.out.println(new Date());
    }
}

Output: Sun Apr 26 16:51:09 IST 2020

Package are divided into two categories:

  1. Built-in packages in java
  2. user-defined packages in java

1. Built in packages in java

Those packages provided by JAVA are known as build-in packages. These packages contain a lot of classes, interfaces, enums, etc. That can be used by the user. There are many built-in packages. Some of the commonly used :

  • java.lang: Here, Java is a top-level package, and lang is a subpackage of java. The lang package contains classes that are fundamental to the design of the Java programming language. The most important class is Object, which is the root of the class hierarchy
  • java.util: Here, Java is a top-level package, and util is a subpackage of java. The util package contains all the utility classes, and it contains the collections framework.
  • java.awt: Here, Java is a top-level package, and awt is a subpackage of java. The awt package contains classes for implementing the components for graphical user interfaces.
Packages in java

In the JAVA language, java is the top package in build-in packages. All sub-packages are part of the java package.

import java.util.ArrayList;
public class EncapsulationExample
{
    public static void main(String args[])
    {
    	ArrayList<String> list = new ArrayList<String>();
    	list.add("Ram");
    	list.add("Sham");
    	list.add("Krishan");
    }
}

Output: [Ram, Sham, Krishan]

How package is working internally

Package in Java is based on directory structure. In directory structure, a directory can contain several sub-directories, folders, and files. Similarly, the package can contain sub-packages, classes, interfaces, etc.

For Example Here java.util.Date is a package. It means it consists of two directories. The Date is a class that is a sub-part of the util package, and util is a sub-package of java.

Build in Packages in java

2. User defined packages in java

A programmer can define their own package, and this package is a known user-defined package. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, etc are related.

Create a package

The programmer can also create the package. To create a package, you have to use the package keyword. You read it in detail.

package packageName; 
package myPackage; 

Important points about the package

1. Every class is existing in a package. If you are not declaring any package the JVM put the class in the default package. A class that existing in the default package means the current working directory. JVM put all the classes in the same directory that haven’t package names.

2. A class can import any number of packages by import statements, but it can use only one package statement.

3. The creation statement of a package should be the first statement of the program. You can’t specify any other statement above the package statement. You can define comments to the above of the package statement.

4. The name of the package must be the same as the directory under which the file is saved.

5. A public class can directly access the package. You can access public classes in another package. Here is the syntax to access the public class
packageName.className

6. There may be a situation when class name conflict may occur. Let’s say we have two packages java.util and java.sqlBoth the packages have a class with the same name that is the Date class. Now suppose a class imports both these packages like this

import java.util.Date;
import java.sql.Date;
public class ExampleOfPackage 
{
    
    public static void main(String arg[])
    {
        System.out.println("Date with time : "+ new Date(0));
        System.out.println("Sql date : "+ new Date(0));
    }
}

It shows an error at compile time “The import java.sql.Date collides with another import statement“.

In this type of scenario, we should use the full qualified name of the class with the package. If we will not use a fully qualified name? Then the compiler will get confused about which class should be used.

import java.util.Date;
public class ExampleOfPackage 
{
    
    public static void main(String arg[])
    {
        System.out.println("Date with time : "+ new Date(0));
        System.out.println("Sql date : "+ new java.sql. Date(0));
    }
}

Output: Date with time: Thu Jan 01 05:30:00 IST 1970
Sql date : 1970-01-01

A real-life scenario in java package example

Let’s say you are creating a website of a University. A university can have multiple colleges in different districts or states.

  • Let’s say the university name is KUK which placed in Kurukshetra city.
  • Some colleges have the same name but different cities, and some have a different name but the same cities.
    • MLN college from Yamunanagar
    • MLN college from Ambala.
    • Khalsa college Ambala.
  • So, if we want to create classes, interfaces, and enumerations for these colleges, we can create these separate based on cities. We can place all the related classes, interfaces and enumeration in packages.
  • If anyone programmer facing any issue with Yamunanagar college’s data, then he/she will not search the file on the whole directory. He/she will directly go through with the package name.
  • Without the package, you can’t create two classes with the same name because they are treated as in the same default package. But we can create classes with the same name but in different packages.
User defined Packages in java
package kuk.yamunanagar;
public class MLN
{  
	 public static void main(String args[])
	 {  
	    System.out.println("Welcome to MLN college");  
	 }  
} 
package kuk.ambala;
public class MLN
{  
	 public static void main(String args[])
	 {  
	    System.out.println("Welcome to MLN college");  
	 }  
}  
package kuk.ambala;
public class Khalsa
{  
	 public static void main(String args[])
	 {  
	    System.out.println("Welcome to Khalsa college");  
	 }  
}  

How package works internally for university:

Adding a class to a Package

We can add more classes to the existing package by using package names at the top of the program and saving it in the package directory. We need a new java file to define a public class, otherwise, we can add the new class to an existing .java file and recompile it.

Sub packages in java

A package can have many classes, interfaces, enums, or sub-packages. When a package is created inside another package is known as a subpackage in java. In our JDK, there are many examples of sub-packages like java.lang here java is package and lang is subpackage, java.util here java is package and util is a subpackage.
These are not imported by default. So, you have to import it explicitly. You can import any class or interface from a subpackage by use of import statements. Let’s read how to access packages.

Leave a Comment