Package Concept in JAVA and Import statements


What are packages and how to create packages and pack classes in it. What If you want to use one package class in another. How to use import statement for the packages.

In this article I'll be covering up some little and quick topics on Java packages. The concept is not so wast to i'll try to keep is as simple as possible. First Let us break down the points I'll be covering up in this post.

  • Understanding Packages
  • Defining a Package
  • Packaging up your Classes
  • Adding Classes from a Package to your Program

So let me now let you know more about these topics under these headings

Understanding packages

Packages in Java are nothing more than a hierarchical structure which is generally there to keep the code and software organized. Package server to Java like a directory serves to any storage management system. In general terms package is also a directory in which you place files. But the difference is that in files which are usually class files, you must mentioned to which package they belong. for example take the instance of the general and most common of the package provided with the JDK, i.e. the java.lang package.

In this statement "java" and "Lang" both are package itself and these contains the classes in them. the "java.lang.String" defines the following structure

java(root package)
	|_lang(another package under Java package)
		|_String(Class under the lang package)

So in this way we can define the Classes which belong to the core of Java language in java.lang package. All the classes you find in there will be related to java language core.

or for example you are making some software for capturing image with your camera and uploading picture to any social networking site. You may place all the files in same directory or you may arrange them properly using package power of Java. Such that

root
|_camera(containing classes related to camera features)
|_upload(classes related to uploading images)
	|_upload.Social(to upload images on any particular social network)
|_User(user information processing related classes)
	|_user.friends(classes to manage the friends connection for user on the software)

so this gives a privilege to the user.

Defining a Package and packaging of class

If you understand, what packages are and where they are used let me show you how easy it is to create a package. And for this consider this directory hierarchy

root
|_pakagebase -> Class:Base
	|_packagelevel1 -> Class:Level1
		|_packagelevel2 -> Class:Level2

Note: These package requires that these must be existing on your file structure in exactly this way.

Now let us make this happen in program


/*
filename - Base.java
this will remain in the pacakgebase directory
*/
pacakge packagebase;
class Base{
	//class definition
}

/*
filename - Level1.java
this will be placed in packagelevel1 directory
*/
package packagebase.packagelevel1;
class Level1{
	//class definition
}

/*
filename - Level2.java
this will be placed in the packagelevel2 directory in the file system.
*/
package packagebase.packagelevel1.packagelevel2;
class Level2{
	//package definition
}

As you can see in the program for every sub package we have to give fully qualified package name starting from base package. and the assigning of class to package in simple as adding a package keyword and package name as the first line of code in the program.

Adding class form Package to your program

The classes are not just packed in class they are also to be used in other classes in other package otherwise there is no use if we can not use it outside the package. this is also simple, for using one class from a package to other we need to import.

For example we need any of above class into some other class of ours we would first need to import it or either to use it with fully qualified package name.


/*
DemoImport.java
*/
import packagebase.*; //import all classes defined in package in package base
import packagebase.packagelevel1.Level1; //import Level1 class from packagelevel1 package.
class DemoImport{
	DemoImport(){
		packagebase.packagelevel1.packagelevel2.Level2 l2 = new packagebase.packagelevel1.packagelevel2.Level2(); //using fully qualified class name in class usage
	}
}

When you want to use some class from one package to another you must import it into the program using its fully qualified package path or using wild card. If you see in the program there are three ways in which you may get the task done. In the first statement all the classes have been imported from the package. Second Import statement only includes the specified class. Finally when I needed to use the Level2 class from the packagelevel2, so I have used the complete package domain to use the class.

Note: usually by convention not by rule the package names are all small letters.