Wednesday, 6 July 2016

Package in Java

A package 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 different folders and 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 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 -

  • Packages are used to organize set of related classes and interface.
  • Packages provide visibility/access control mechanism.
  • Packages prevent naming collision.

Types of packages

Packages 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, 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

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

It's general form is
package package_name;

As 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

You can also have hierarchy of packages, in that case package names will be separated by a period (.).

As example

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

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

Importing packages

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 the 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

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

Use the fully qualified name

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!

Related Topics

  1. Class in Java
  2. Access modifiers in Java
  3. Encapsulation in Java
  4. Constructor chaining in Java
  5. Inheritance in Java
  6. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

1 comment: