Till now, we have practiced lots of Java programs, created so many Java Classes. But all these programs, reside somewhere in the local systems. Knowledge about creating new Java Classes, using access modifiers and methods is not enough, one needs to know to How to manage these all Classes together and use the access modifiers with them, to make their accessibility amongst other class files, easy.
Fortunately, Java provides a very great mechanism for allowing easy manipulating techniques for such task, Java Packages. As the name suggests, these are nothing but a pack(group) of classes. In this tutorial, mainly focusses upon Java Packages, What are Packages in Java, How to use Java Packages and different ways of accessing Java Packages etc.
What are Packages in Java?
In Java, the mechanism used to encapsulate all these Java Classes along with other entities, like interfaces and other sub-packages etc., is known as package. It is actually a group of related Java Classes or interfaces providing them access protection and namespace management. Or we can say, it is like a directory/folder to store these Classes, interfaces and even sub-packages.
For example: In Eclipse IDE, you created a package named MyPackage
and now you have to create a class, named myClass inside this package.
This makes it, MyPackage.myClass;
Types of Packages in Java
There are 2 types of packages in Java: Inbuilt Java Packages and User-defined Java Packages
> Inbuilt Java Packages: These are the Java packages which come pre-defined along with the Java Development Kit(JDK). These packages already exist in the JDK such as java.util.*, java.lang.*, java.io.* etc.
This format is quite simple to understand. Let us consider the example: java.util.*
Here the package name is Java, which has a sub-package named util, which further has some classes inside it. But here, the use of asterisk(*) symbol signifies all classes. So here, we are denoting to all the classes inside the util sub-package of the java package.
For considering single class in this package: java.util.ArrayList, where java is the package, util is sub-package and ArrayList is the class.
* You can import all the classes in your programs, whenever necessary.
> User-defined Java Packages: These are the packages, that the user create himself for storing the different class files. These user-defined packages are stored as a file system directory in the local system.
Example:
Creating Packages in Java
In Java, you just need to use the package keyword to create a package.
Syntax: package package-name;
Example: package MyPackage;
* This statement creates a package named MyPackage. And if this statement is included in any Java program at the beginning of the program. Then that class file will automatically be stored in that Java Package.
* This package declaration statement should be the very first statement of the program by convention.
Program :
package MyPackage1;
public class MyClass{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
* This creates a class MyClass inside MyPackage1 package.
* We can have only 1 package declaration in a class. Meaning we cannot do the following thing in Java
package MyPackage1;
package MyPackage2; //ERROR
public class MyClass{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
This will generate an error. Hence, 1 class can be included into only 1 package at a time.
Subpackage in Java
A package inside a package is known as a sub-package in Java.
To create a sub-package in Java, we use the period(.) sign. The name of the package followed by a period symbol(.), which is further followed by the sub-package name.
Syntax: package package-name.subpackage-name;
Example: package MyPackage1.MySubPackage1;
* This creates a sub-package named MySubPackage1 inside the package MyPackage1.
Program :
package MyPackage1.MySubPackage1;
public class A{
public static void main(String[] args){
System.out.println("Hello World 2nd Time!");
}
}
* This creates a class A inside the sub-package MySubPackage1 of MyPackage1 package.
Importing Packages in Java
To import a package in Java, we use the import keyword. This is useful in importing any package, sub-package, class, interface or any other entity. After importing a java package, it can be used from any other package, or even outside the package, with the help of objects.
You need to create the object of the required class present in that package. And then you can easily access the functionalities of that class.
Syntax:
import package-name; -> for importing a complete package
import package-name.subpackage-name; -> for importing a sub-package inside a package
import package-name.class-name; -> for importing a particular class inside a package
import package-name.subpackage-name.class-name; -> for importing a class inside a sub-package which is further inside a package
* We can import as many as packages in a program, but the import statements should always come after the package declaration statements only.
> Importing a inbuilt Java class
import java.util.Scanner;
public class B{
public static void main(String args[]){
Scanner object1 = new Scanner(System.in);
int x = object1.nextInt();
System.out.println(x+" is the input");
}
}
OUTPUT
4
4 is the input
* Here, we have import Scanner Class from the util package java.util, and then in order to access the methods and functionalities of the Scanner Class, we created an object named object1, which used the nextInt() method of the Scanner class to take an integer input from the user.
* We could also have used import java.util.*; but in this case all the classes inside the sub-package util would have been imported inside the program, which would affect the compiler performance.
> Creating and Importing a user-defined package in Java
package PackageA;
class A{
public void method1(){
System.out.println("Inside Class A");
}
}
package PackageB;
import PackageA.*;
public class B{
public static void main(String[] args){
A obj1 = new A();
obj1.method1();
}
}
OUTPUT
Inside Class A
* import PackageA.* imports all the classes inside PackageA package.
> Importing a particular Class inside a package
package PackageAA;
class ClassAA{
public void method11(){
System.out.println("Inside Class AA");
}
}
package PackageBB;
import PackageAA.ClassAA;
public class ClassBB{
public static void main(String[] args){
ClassAA obj1 = new ClassAA();
obj1.method1();
}
}
OUTPUT
Inside Class AA
* import PackageAA.ClassAA; imports only the class ClassAA of the PackageAA package.
> Importing packages with fully qualified names
This is the only method of importing packages, where we need not use the import keyword.
package PackageA;
class A{
public void method1(){
System.out.println("Inside Class A");
}
}
package PackageB;
public class B{
public static void main(String[] args){
PackageA.A obj1 = new PackageA.A();
obj1.method1();
}
}
OUTPUT
Inside Class A
* Here we have used a fully qualified name to import the particular class of the package, without the use of import keyword. The statement PackageA.A obj1 = new PackageA.A(); itself creates an object obj1 for the class A of PackageA package.
We can also use the method of fully qualified names with sub-packages also.
Example:
package PackageA.SubPackageA;
class A{
public void method1(){
System.out.println("Inside Class A of SubPackageA");
}
}
package PackageB;
public class B{
public static void main(String[] args){
PackageA.SubPackageA.A obj1 = new PackageA.SubPackageA.A();
obj1.method1();
}
}
OUTPUT
Inside Class A of SubPackageA
** This method of fully qualified names is very useful in cases where, different packages having classes with same class-names are imported together in a program. If we donot use the fully qualified name here, then a compilation error would be thrown by the compiler.
Compiling and running packages from Command Line
Consider the following program, saved by the name of MyClass.java file
package MyPackage1;
public class MyClass{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
To compile this java file inside the MyPackage1 package, use the following syntax:
Syntax: javac -d directory file-name.java
* Here -d is used to change to the location where we tend to store the class file of the related java file. And directory is the place/location where the class file of the .java file would be saved. You can specify the directory using /home/User/Desktop format or you save it in the current directory, which is denoted by period(.) symbol.
Example: java -d . MyClass.java
* Here the class file would be saved in the current directory only.
To run this class file, simply write
java package-name.class-name
Example: java MyPackage1.MyClass
* This would run the Class file and print the output on the command line.