Tuesday, June 15, 2021

Package in Java

In this tutorial you'll learn how to create packages in Java and how to import them.

Benefits of using packages

A package in Java is a namespace that organizes a set of related classes and interfaces. Packages also help in preventing naming conflicts.

Conceptually you can think of packages as being similar to folders in OS. You can have files with similar names in different folders same way in different packages there can be Java classes with the same name thus preventing name collision as class is qualified with package name.

Packages in Java also provide visibility control mechanism. You can define classes and class members inside a package that are not visible to the classes in other packages.

So to summarize the advantages of package in Java-

  • Packages are used to organize set of related classes and interface which makes its easy to find a specific class, interface.
  • Packages provide visibility/access control mechanism. With default access a member is visible only within its own package. Protected modifier specifies that the member can only be accessed within its own package and by a subclass of its class in another package.
  • Packages prevent naming collision. Since class is qualified with package name so you can have class with same name in different packages, for example org.netjs.Test and org.netjs.examples.Test.

Types of packages

Packages in Java can be of two types-

  • Built-in packages- In Java there are many built-in packages where classes are grouped according to the functionality. As example- java.lang which provides classes that are fundamental to the design of the Java programming language, java.io which provided classes for system input and output, java.util which contains collections framework among other things.
  • User defined packages- Packages created by users to group their classes/interfaces according to the functionality.

Creating a package in Java

To create a package just include package command as a first statement in your Java source file.

General form of a package in Java is as follows-

package package_name;

Java package creation example

package testpackage;
public class Test{
 public static void main(String[] args) {
  Test t = new Test();

In this example code first statement package testpackage; creates the package testpackage if it is not already created. If package is already there then the class Test will also become part of that package, here note that more than one file can belong to the same package.

If you are using some editor like Eclipse then you can create package using new package option with in a Java project.

Hierarchy of packages in Java

You can also have hierarchy of packages where a sub-package resides inside another package. In that case package names will be separated by a period (.).

package org.netjs.prog;
public class Test{
 public static void main(String[] args) {
  Test t = new Test();

In this case Test.java will be stored under the folder structure \org\netjs\prog in windows environment.

Importing packages in Java

Packages are a good way to group set of related classes and interfaces, but you do need to use classes/interfaces from one package in another package. If you want to access classes with in the package then you need to use the fully qualified name i.e. class name along with package name. That will mean a lot of typing just to access a class as example - pkg1.pkg2.pkg3.ClassName

To avoid that Java has import statement which can make visible a specific class or a whole package. In Java source file import statement must be written directly after package statement.

To sum it up there are three options when you want to use classes/interfaces from another package -

  • Import the whole package
  • Import specific classes
  • Use the fully qualified name

Import the whole package

As example if you want to use List interface, which resides in java.util, in your class then you can import the whole util package.

import java.util.*;

In that case whole java.util package is imported which apart from List interface contains many other classes and interfaces.

Import specific classes

If you have a statement like this in your source file

List<String> ls = new ArrayList<String>();

Then you need to have two import statements for importing classes from the package.

import java.util.ArrayList;
import java.util.List;

Use the fully qualified name

You can also use the fully qualified name of the class which includes the package name along with the class. In that case the same statement will look like-

java.util.List<String> ls = new java.util.ArrayList<String>();

That's all for this topic Package in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page

Related Topics

  1. Class in Java
  2. Encapsulation in Java
  3. Constructor chaining in Java
  4. Inheritance in Java
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Difference Between Abstract Class And Interface in Java
  2. final Keyword in Java With Examples
  3. Difference Between equals() Method And equality Operator == in Java
  4. How HashMap Works Internally in Java
  5. Interface Default Methods in Java
  6. Callable and Future in Java With Examples
  7. Count Number of Words in a String Java Program
  8. Print Odd-Even Numbers Using Threads And wait-notify Java Program