Packages in Java – Know their Importance in Java !!

In our computer, we have folders for the classification and the accessibility of various files, but in Java we have packages. Packages In Java are mainly used to organize classes and interfaces it helps us to write better and manageable code. There are some built-in packages available to use but, we can create our own (user-defined).

So, don’t waste the time and jump-start the concept.

1. Packages in Java

A package in Java is a group of similar types of classes, interfaces, and sub-packages. They can be categorized into two categories, the built-in package ( java, lang, util, awt, javax, swing, net, io, sql et), and user-defined package.

Java Packages Sub-packages classesDon’t forget to check how classes are different from Interfaces in Java? 

They are used for the following tasks –

  • To prevent the naming conflicts which can occur between the classes.
  • Make the searching and locating of classes or enumerations or annotations much easier.
  • Provide access control to the classes.
  • Used for data encapsulation.

 

Example of java package-

We can create a java project using package keyword.

package com.dataflair.packageprogram;//package

class SimpleProgram
{
  public static void main(String args[])
  {
    System.out.println("Hello Reader, Welcome to DataFlair!");
  }
}

Output-

simple-package-program

Recommended Reading – Annotations in Java with real-time example

2. Advantages of Java Package

  • A Java package is mainly used for the categorization of classes and interfaces so that we can maintain them easily.
  • They always provide access protection
  • Used to bundle classes and interface.
  • With the help of packages, we can reuse the existing code
  • By using package we can easily locate the classes related to it.
  • Also, remove the naming collision.

3. Working on Packages in Java

To work on Java packages, 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. The main idea behind this is to locate these classes easily.

  • Package naming conventions

They are conventionally named with their domains in reverse order.

  • Adding a class to a Package

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

  • 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.

It’s the right time to revise Access Modifiers in Java with Example

Example 

import java.util.*;
Java Quiz

4. Accessing Classes Inside Java Package

If we want to access all the classes and interfaces of a package then we use .*, by using .* we can access all the classes of this package but not the sub-packages.

Example of the package that imports the package name.*

package com.dataflair.packageprogram;

public class Class1 
{
  public void message()
  {
    System.out.println("Hello DataFlair");
  }
}
package com.dataflair.packageprogramdemo;
import com.dataflair.packageprogram.*;
public class Class2 
{
  public static void main(String args[])
  {  
    Class1 obj = new Class1();  
    obj.message();

  }
}

Output-

Accessing-Package

Let’s explore the trending difference – Core Java Vs Advanced Java 

5. Types of Packages in Java

There are two types of Packages in Java-

two types of Java Packages

5.1 Built-in Packages in Java

Built-in is a part of Java API and it offers a variety of 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.

5.2 User-defined Packages in Java

In Java, the user creates these packages according to their requirements. For example –

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

package com.dataflair.packageprogram;

public class MyClass
{
  public void getNames(String name)
  {
    System.out.println(name);
  }

}
package com.dataflair.packageprogramdemo;
import com.dataflair.packageprogram.MyClass;
public class MyClass1 {
  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);
  }
}

Output-

user-defined-packages in Java

Have you checked the current career opportunities of Java?

6. Summary

In this tutorial for Java, we studied about packages in Java, their working, how to access packages in other classes, types of packages with example. This will help us understand and move further with our learning of the Java language.

Still, if you have any queries, feel free to ask through the comment section.

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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.