Packages in Java are a way to pack(group) a number of related types such as classes, interfaces, enumerations, and annotations together into a single unit. Think of it as a container for the classes.
Packages are used to avoid name conflicts, provide a good structure to the projects with maintainable code that consists of many classes.
Reason to use Packages(advantages):
- It provides the reusability of the same code again and again in the program by creating a class inside a package for that particular task.
- The name of the two classes can be defined by the same name in two different packages and we can avoid name conflicts using packages.
- We can hide the classes in packages if the user doesn’t want it to be accessed by other packages.
- Hundreds of classes used in packages are organized in a better way so that when needed it can be easily accessed by the programmer.
Creating Package in Java:
We can create a package in java by declaring the keyword ‘package’ followed by the ‘packageName‘.
Syntax:
1 | package mypack; |
Syntax to compile the program.
1 | javac -d Destination/Directory_folder file_name.java |
Note:
The -d in here specifies the destination/directory to put the generated class file.
Other directory names can be used such as d:/xyz (in case of windows) etc.
And to keep the package within the same directory we need to use. (dot).
Syntax to run the Java Program:
1 | java package_Name.Class_Name |
Let us see it in an example:
1 2 3 4 5 6 7 8 9 | //save as CreatingPack.java package pack1; public class CreatingPack { public static void main(String args[]) { System.out.println("Hello! I am Package"); } } |
To compile:
1 | javac -d . CreatingPack.java |
To run:
1 | java pack1.CreatingPack |
Output:
1 | Hello! I am Package |
import Keywords:
If the user needs to access or use the class of some other package in his/her current package, then it can be done with the help of the import keyword. import keyword imports the packages with reference to all classes or a particular class of that imported package.
This keyword is used to import both Built-in Packages and User-defined Packages. You can see the use of import below in examples.
Accessing Classes from Packages:
It can be done in the following ways,
1. Using fully qualified name:
When we use a fully qualified name then we do not need to use import but need to use a fully qualified name every time we want to access the class or interfaces of that other package.
Example of Using fully qualified name in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //save by Class1.java package pack1; public class Class1 { public void display() { System.out.println("Display Accessed"); } } //save by class2.java package mypack2; class class2 { public static void main(String args[]) { pack1.Class1 obj = new pack1.Class1(); //using fully qualified name obj.display(); } } |
Output:
1 | Display Accessed |
2. import only selected class from a package:
This is the way in which the user can only access those classes that are required from the package. This is done by import packageName.className
Example of importing only selected class from a package in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //save by Class1.java package pack1; public class Class1 { public void display() { System.out.println("Display Accessed"); } } //save by Class2.java package pack2; import pack1.Class1; //class1 is accesed from pack1 package class Class2 { public static void main(String args[]) { Class1 obj = new Class1(); obj.display(); } } |
Output:
1 | Display Accessed |
3. import all the classes from a selected package:
‘*’ is used instead of className when the package is imported. But the classes and packages that are present inside the sub-packages cannot be accessed. This can be done by import packageName.*;
Example of import all the classes from a selected package in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //save by Class1.java package pack1; public class Class1 { public void display() { System.out.println("Display Accessed"); } } //save by Class2.java package pack2; import pack1.*; //importing all classes from pack1 package class Class2 { public static void main(String args[]) { Class1 obj = new Class1(); obj.display(); } } |
Output:
1 | Display Accessed |
Sub-Packages:
The package within another package is called sub-package.
Example of Sub-packages in Java:
If we create a package called ‘Add’ inside a package named ‘AddNumber’ then the Add is called sub-package and it will be declared as package AddNumber.Add
1 2 3 4 5 6 7 8 | //saved Addition.java public class Addition { int sum(int x, int y) { return x*y; } } |
And to use this class present in a sub-package we can either import the package or use fully qualified name.
Importing a package:
1 | import AddNumber.Add.Addition; |
using a fully qualified name:
1 | AddNumber.Add.Addition obj = new AddNumber.Add.Addition(); |
Categories of packages:
- Built-in Packages (Java API)
- User-defined Packages (self-created)
1. Built-in Packages (Java API) :
These are already defined packages in Java. Java APIs are the already defined classes that are provided for free use.
The mostly use built-in packages are:
- java.lang
Contains classes for primitive data types, strings, math operations, threads, and exception. - java.util
contains utility classes such as vectors, hash tables, dates, etc. - java.io
Stream classes for input/output operations. - java.awt
Contain classes for implementing GUI such as buttons, menus, etc. - java.net
Classes for networking operations. - java.applet
Contain classes for creating and implementing applets.
Example: If the user input is required, we import Scanner and following is the way to do it:
1 | import java.util.Scanner |
2. User-defined Packages (self-created):
These packages are created/defined by the user themself. Users need to create a directory ‘egPackage‘ and name should match with the name of the package. Then the user can create a class (EgClass) inside that directory by declaring the first name as the package name.
1 2 3 4 5 6 7 8 9 10 | // Name of the package must must match with the directory where the file is saved package egPackage; public class EgClass { public void detail(String s) { System.out.println(s); } } |
And now the user can use this package in another program by importing it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //importing the class 'EgClass' from egPackage import egPackage.EgClass; public class DisplayClass { public static void main(String args[]) { String disp = "Diplaying the details"; EgClass obj = new EgClass(); obj.detail(disp); } } |
Output:
1 | Diplaying the details |