Packages in Java – Working & Types of Java Packages With Examples

1. Packages in Java

In this Java Packages tutorial, we are going to learn about the packages in Java, how they are accessed. Moreover, we will discuss the working of Packages in Java Programming. Along with this, we will cover different types of Packages in Java with example Program.

So, let’s start to learn Packages in Java.

Introduction to Packages in Java

Introduction to Packages in Java

2. What are the Packages in Java?

Packages in Java are a way to encapsulate interfaces, Java classes and also subclasses. They are used for the following tasks –

  • Java Packages are used to prevent the naming conflicts which can occur between the classes.
  • Packages in Java make the searching and locating of classes or enumerations or annotations much easier.
  • Java Packages provide access control to the classes.
  • Packages in Java are used for data encapsulation.

3. Working of Packages in Java

For packages to work we need to classify the similar or related class in a package and then we call it whenever we need it. Names of packages and structure of directory are very closely related. An example of this would be a package named college.staff.cse, here the three directories college, staff and cse belong to each other like, cse belongs to staff and staff belongs to college. The main idea behind this is to locate this classes easily.

a. Package naming conventions

Packages in Java are conventionally named with their domains in reverse order.

b. Adding a class to a Package

For adding more classes to Java packages we can add the name of the package and save it the directory of the package.

c. Sub-packages

Just like a subclass a subpackage is a package inside another package, they can be imported implicitly and also they cannot have access modifiers.
Example 

import java.util.*;

4. Accessing Classes Inside Java Package

// Java program to demonstrate accessing of members when corresponding classes are imported and not imported.
import java.util.Vector; 
public class ImportDemo
{
   public ImportDemo()
   {
      // java.util.Vector is imported, hence we are able to access directly in our code
      Vector newVector = new Vector();  
      // java.util.ArrayList is not imported, hence We were referring to it using the complete package
      java.util.ArrayList newList = new java.util.ArrayList();
   }  
   public static void main(String arg[])
   {
      new ImportDemo();
   }

This import java.util.*;imports all the packages from util.

Let’s discuss the Serialization and Deserialization in Java

5. Types of Packages in Java

There are two types of Packages in Java

Types of Packages in Java

Types of Packages in Java

a. Built-in Packages in Java

Built-in Java packages are a part of Java API and it offers a variety of packages, list of java packages are –

  • lang – Automatically imported and it contains language support classes.
  • io – Contains classes for input and output operations.
  • util – Contains utility classes for implementing data structures.
  • applet – This package contains classes that create applets.
  • awt – Contain classes that implement compounds for GUI.
  • net – This package contains classes that support networking operations.

Read about Types of Operators in Java

b. User-defined Packages in Java

Java User creates these Java packages. For example –

We first create a directory named myPackage and then a class named myClass in it.

// Name of the package must be same as the directory under which this file is saved
package myPackage;
public class MyClass
{
    public void getNames(String s)
    {
        System.out.println(s);
    }
}
/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;
public class PrintName
{
   public static void main(String args[])
   {
      // Initializing the String variable  with a value
      String name = "DataFlair";
      // Creating an instance of class MyClass in the package.
      MyClass obj = new MyClass();
      obj.getNames(name);
   }
}

6. Import Static

Static imports feature introduce in Java from version 5. It allows the user to define a class public static.
Example
import static java.lang.System.*;

class StaticImportDemo
    {
       public static void main(String args[])
            {
// We don't need to use 'System.out'
// as imported using static.
                  out.println("DataFlair");
            }
      }

7. Handling Name Conflicts

In the example below java.util and java.sql packages have a class named Date.
So,

import java.util.*;
import java.sql.*;
// we will get a compile time error
Date today ; //ERROR-- java.util.Date or java.sql.Date?

We can solve this Problem using keyword import.

import java.util.Date;
import java.sql.*;

Read more about Java Exception Handling

So, this was all about Java Packages Tutorial. Hope you like our explanation.

8. Conclusion

In this tutorial for Java, we studied about packages in Java, their working, how to access Java Packages, types of Packages in Java with example Program, and many other things. This will help us understand and move further with our learning of the language. Still, if you have any query, feel free to ask through the comment section.

See Also- Java Installation

For reference

4 Responses

  1. priya says:

    A package is a grouping of related types providing access protection and name space management. Many thanks for sharing this blog.

    • Asif khan says:

      Packages is the collection of classes sub packages and interfaces..

      • Data Flair says:

        Hi Asif,
        We glad you provide such a brief information to other readers. We hope you explore other Java tutorials, we refer you to participate on this Java Quiz
        Regards,
        Data-Flair

    • Data Flair says:

      Hi Priya,
      Thanks for sharing information with us on. It will help other readers to understand the definition of Packages in Java.
      Visit Data-Flair to learn java Programming Language.

Leave a Reply

Your email address will not be published. Required fields are marked *