Java Packages: Organization and Usage Explained
Table of Content:
What is packages in java
Java package is a group of similar types of classes, interfaces and sub-packages or we can say Packages in Java is a mechanism to encapsulate a group of classes, interfaces and sub packages which is used to providing access protection and namespace management and to make searching/locating and usage of classes, interfaces, enumerations and annotations easier.
Package in Java 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, javax, swing, net, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.
Types of package:
- Built-in Package:-Existing Java package for example,
java.io.*,
java.lang
java.util
etc. - User-defined-package:- Java package created by user to categorized classes and interface
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) In real life situation there may arise scenarios where we need to define files of the same name. This may lead to name-space collisions. Java package removes naming collision.
4) Reusability: Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program.
5) Easy to locate the files.
Creating a package
Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.
This is the general form of the package statement:
package pkg;
Here, pkg is the name of the package. For example, the following statement creates a package called myPackage.
package myPackage;
package mypackage; public class Student { ...statement; }
The above statement create a package called mypackage.
Java uses file system directory to store package. For example the .class
file for any
classes you define to be part of mypackage package must be stored in a directory
called mypackage.
Points to Remember for package:
- A package is always defined in a separate folder or directories having the same name as a package name.
- Define all classes in that package folder or directories.
- All classes of the package which we wish to access outside the package must be declared public.
- All classes within the package must have the package statement as its first line.
- All classes of the package must be compiled before use (So that its error free)
Example of java package
The package keyword is used to create a package in java.
Program:
//save as PckProgram.java package mypackage; public class PckProgram{ public static void main(String args[]){ System.out.println("Welcome to package"); } }
Points to remember:
1. At most one package declaration can appear in a source file.
2. The package declaration must be the first statement in the unit.
How to compile java package
You need to follow the syntax given below:
javac -d directory javafilename
For example
javac -d . PckProgram.java
The -d switch specifies the destination where to put the generated class file. You can use any directory name like d:/abc (in case of windows) or like /home (in case of Linux), d etc. If you want to keep the package within the same directory, you can use . (dot).
How to run java package program?
You need to use fully qualified name e.g. mypackage.PckProgram
to run the class.
To Compile: javac -d . PckProgram.java
To Run: java mypackage.PckProgram
Output:
Welcome to package press any key to continue...
The -d is a switch that tells the compiler where to put the class file i.e. it represents
destination. The .(dot) represents the current folder.
if you want to create clas file in child folder the use double dot that is ..
javac -d .. PckProgram.java
You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as
package java.awt.image;needs to be stored in java\awt\image in a Windows environment. Be sure to choose your package names carefully. You cannot rename a package without renaming the directory in which the classes are stored.
How to access package from another package?
import keyword is used to import built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.
There are three ways to access the package from outside the package.
- fully qualified name,
- import package.*;
- import package.classname
1. Using fully qualified name(But this is not a good practice.)
If you use fully qualified name then only declared a class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
//save by ClassA.java package pack; public class ClassA{ public void msg(){ System.out.println("Hello Brother"); } }
//save by ClassB.java package mypack; class ClassB{ public static void main(String args[]){ pack.ClassA obj = new pack.ClassA();//using fully qualified name obj.msg(); } }Output:
Note: If you import a package, subpackages will not be imported.
Hello Brother
2. Using packagename.classname (import the only class you want to use)
import the only class you want to use(Using packagename.classname) If you import package.classname then only declared class of this package will be accessible.
//save by ClassA.java package pack; public class ClassA{ public void msg(){ System.out.println("Hello Brother"); } }
//save by ClassB.java package mypack; import pack.ClassA; class ClassB{ public static void main(String args[]){ ClassA obj = new ClassA(); obj.msg(); } }Output:
Note: If you import a package, subpackages will not be imported.
Hello Brother
3. import all the classes from the particular package(Using packagename.*)
If you use package.*
then all the classes and interfaces of this package
will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
//save by ClassA.java package pack; public class ClassA{ public void msg(){ System.out.println("Hello Brother"); } }
//save by ClassB.java package mypack; import pack.*; class ClassB{ public static void main(String args[]){ ClassA obj = new ClassA(); obj.msg(); } }Output:
Note: If you import a package, subpackages will not be imported.
Hello Brother
Classpath :
It is an environmental variable, which contains the path
for the default-working directory (.).
The specific location that java compiler will consider,
as the root of any package hierarchy is, controlled by Classpath
Points to remember
- When a package name is not specified, a class is defined in the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
- While creating a package, care should be taken that the statement for creating package must be written before any other import statements.
// not allowed import package p1.*; package p3;
//correct syntax package p3; import package p1.*;