Packages

A package is the collection of related classes, interface and sub packages.

A package in java is used to categorize and group classes.

For example, we organize and manage different types of files such as audio files, video files and document files in separate folders in the computer for better arrangement and accessibility as well as removing the naming conflicts among the files.

Java uses a packages for organizing and managing related classes.

The advantages of packages includes:

  • Classes defined in a package of an application can be reused any number of time by any other applications
  • A package can contain only unique classes. Therefore using packages helps to ignore naming conflicts among classes.
  • Protection level can be improved by using appropriate access specifier. That is, if we do not want other packages to access class defined in a particular package we can assign them package scope.
  • Packages also helps to separate design from coding.
  • Java manage packages in a hierarchy therefore code is more streamlined.

Package can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, io, util etc.

package-java

A class defined in any package can be referenced through fully qualified name such as java.lang.System , here java is the package, lang is the sub package and System is the name of the class.

Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes and interfaces are related.

Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classes.

As we find it easier to locate an audio file in the computer if we have kept all audio file in a separate folder so as video and documents because all similar type of files are kept in a separate folder.

Creating Package

Java provides package keyword to create a package. In order to create a package we must provide a name for the package after the package keyword as:


								package packageName; 
								

If present, the package statement must be the first no-comment statement in the source files of a java program. There can be only one package statement per source (.java) file. It is a good practice to name a package in lowercase to avoid the conflicts with the class and interface name.

Example
We have to create a Student Management System (SMS) which includes many classes such as Student, Course, Account, Faculty etc. We can group all the classes of Student Management System into a single package sms.


package sms;
//there can be some import statements here. 
public class Student {
	//members 
}
public class Faculty {
	//members 
}
public class Account {
	//members 
}

Sub-Package

Sub packages, are the packages inside a package to further categorize and group a package.

For example Sun Micro system has defined a package named java that contains many classes like System, String, Reader, and Writer etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/output operation, Socket and ServerSocket classes are for networking etc and so on.

So, Sun has sub categorized the java package into sub packages such as lang, net, io etc. and put the Input/output related classes in io package, Server and ServerSocket classes in net packages and so on.

The sub package’s name is separated by a dot (.) in the package declaration. For example in java.lang or java.io, lang and io are the sub packages of java package.

A sub package can be defined as:


							package packagename.subpackagename.subpackagename
							

Java stores the class into a directory for packages and sub-directory for sub-package. All the class files belonging to one package are stored in a folder named with the package name.

When we compile our program compile out java program all the class files are created in the same directory as the source directory.

However we can redirect the class files into another directory using –d option with javac compiler. The –d option allow us to specify the path in which we want the class file to be stored.

It can be used as follows:


javac –d path filename.java 

In the above command, javac for compilation, path specifies the drive letter, folder and sub folder name where class file is to be stored and filename.java specifies the source file.


Example: javac –d c:\mypackage Test.java 

The above command will create a Test.class file in the mypackage folder in the c drive. Assuming that Test.java has only one class Test.

Creating Jar files for Packages

We can create a zip folder (jar files) and store it in jre/lib/ext directory in order to reuse classes defined in package.

Create jar files :


								jar cf mypackage.jar *.java 
								

In the above command,

  • jar is a command-line utility to create jar files
  • Option c tell the command line to create a new archive.
  • The option f tells it to create a file which is followed by a file name (mypackage.jar in our case).
  • *.java means all the source files with .java extension it specifies the source files whose class files will be created and archived into mypackage.jar file.

Now create a .zip file for the mypackage.jar file and copy it to jre/lib/ext directory. Package is ready to be reused.

Using a package in Java

A package can be used by other package using import statement. Java provides a keyword called import which is used to specify the name of the package to be imported in the new program in order to use classes defined in the earlier packages.

Import statement can appear in a java program after package statement. The structure of the import statement is as follows.


Import package.classname; // for importing a specific class of the package 
Or  
Import package.*; // for importing all the classes of the package. 

Whenever we write a java program a package (import java.lang.*;)present in java library is imported by default. The java.lang package contains all the basic classes such as String, System etc.

Therefore, enables us to use those classes.


package Demo;
//importing all the classes present in the SMS package. 
import SMS. * ;
class Test {
	public static void main(String ar[]) {
		/* because we have imported all the classes of the SMS package we can create and use the the instances of all the classes such as Student, Account and Faculty; */
		Student s = new Student();
	}
}

Note that, before importing the package the class path must be set. Otherwise compiler will not be able to load the classes and result into an error.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group