Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Packages. No Comments on Java Packages

In this article we will look at Java packages which are a way to create namespaces. We will learn how to create a package and how to access Java packages.

 

Package Definition

 

A package is a group of related classes. A package is used to restrict access to a class and to create namespaces. If all reasonable class names are already used, then we can create a new package (new namespace) and reuse the class names again. For example a package mypackage1 can contain a class named MyClass and another package mypackage2 can also contain a class named MyClass.

 

Defining or Creating a Package

 

A package can be created by including the package statement as first line in a Java source file. Syntax for creating or defining a package is given below:

package  mypackage;

 

In the above syntax, package is a keyword and mypackage is the name of our package. All the classes that follow the package statement are considered to be a part of the package mypackage.

 

Multiple source files can include the package statement with the same package name. Packages are maintained as regular directories (folders) in the machine. For example, we have to create a folder named mypackage and store the .class files in that folder.

 

Following example demonstrates creating a package:

 

ClassA.java

package mypackage;

public class ClassA 
{
	public void methodA()
	{
		System.out.println("This is methodA in Class A");
	}
}

 

ClassB.java

package mypackage;

public class ClassB 
{
	public void methodB()
	{
		System.out.println("This is methodB in Class B");
	}
}

 

In the above example, both ClassA and ClassB belong to the same package mypackage. Remember that the package creation statement should be the first line in the source file.

 

We can also create a hierarchy of packages as shown below:

package pack1.pack2.pack3;

 

Remember that packages are normal folders in the file system. So, pack3 is a sub folder in pack2 and pack2 is a sub folder in pack1.

 

Java Packages and CLASSPATH

 

It is not mandatory that the driver program (main program) and the package(s) to be at the same location. So how does JVM know the path to the package(s)? There are three options:

  1. Placing the package in the current working directory.
  2. Specifying the package(s) path using CLASSPATH environment variable.
  3. Using the -classpath or -cp options with javac and java commands.

 

If no package is specified, by default all the classes are placed in a default package. That is why no errors are shown even if we don’t use a package statement.

 

By default Java compiler and JVM searches the current working directory (option 1) for specified package(s) like mypackage. Let’s assume that our package mypackage is stored at following location:

 

D:\packages\mypackage

 

Then we can set the CLASSPATH (option 2) environment variable (in command prompt) to the location of the package as shown below:

 

set CLASSPATH = .;D:\packages

 

The dot (.) before the path specifies the current working directory and multiple paths can be separated using semi-colon (;).

 

We can also use -classpath or -cp options (option 3) with javac and java commands as shown below:

 

javac  -classpath  .;D:\packages  Driver.java

or

javac  -cp  .;D:\packages  Driver.java

 

In the above example, Driver.java is our main program which utilizes the classes in the package mypackage.

 

Importing or Using Packages

 

There are multiple ways in which we can import or use a package. They are as follows:

  1. Importing all the classes in a package.
  2. Importing only necessary classes in a package.
  3. Specifying the fully qualified name of the class.

 

First way is to import all the classes in a package using the import statement. The import statement is placed after the package statement if any and before all class declarations. We can import all the classes in a package as shown below:

import  mypackage.*;

 

* in the above line denotes all classes within the package mypackage. Now you are free to directly use all the classes within that package. A program which demonstrates importing all classes in a package is given below:

import mypackage.*;

public class Driver
{
	public static void main(String[] args) 
	{
		ClassA obj1 = new ClassA();
		obj1.methodA();
		ClassB obj2 = new ClassB();
		obj2.methodB();
	}
}

 

If you want to use only one or two classes in a package, the second way is to specify the class names instead of * as shown below:

import  mypackage.ClassA;

import  mypackage.ClassB;

 

A program which demonstrates importing specific class from a package is given below:

import mypackage.ClassA;

public class Driver
{
	public static void main(String[] args) 
	{
		ClassA obj1 = new ClassA();
		obj1.methodA();
	}
}

 

Suppose if two packages contain a class with same name, then it will lead to compile-time errors. To avoid this, we have to use the fully qualified name of the class. A fully qualified name of the class refers to the name of the class preceded by the package name. An example that demonstrates a fully qualified name is given below:

public class Driver
{
	public static void main(String[] args) 
	{
		mypackage.ClassA obj1 = new mypackage.ClassA();
		obj1.methodA();
	}
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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