Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as the category they should belong to.
It’s easy to organize the files into packages. The packaging also helps us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called “Vector”, its name would crash with the Vector class from JDK.
However, this never happens because JDK uses java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as “Vector” or we can put it into another package like com.easybix.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of the package will also help us manage and use files stored in jar files in more efficient ways.
How to create Package ?
Package creation in java is very easy you just need to type package keyword and package name in the very first line of your program.
- The package declaration must be the first statement in the class.
- At most one package declaration can appear in the source file.
Naming Conventions :
The global naming convention has been proposed to use the internet domain names to uniquely identify packages. Companies use their reversed Internet domain name in their package names, like this:
Video Tutorial of Automation Testing:
How to use Java package ?
Import Package – If you want to use one package members into another package then you need to import the base package first. You can import the package by using the keyword “import” . You can import a package into another package like-
In the above example, we are importing the Java .net package to manage the Network activities in our application.
Products from Amazon.in
Price: Check on Amazon
Price: INR 29,950.00
Price: INR 15,390.00
There are two ways to importing the package.
- Import the single member of the package.importcom.Easybix.Corejava.ArrayTraversing;
- Import all members of the package using ‘*’ .
when we use *, only the classes in the package referred are imported, not the classes in the sub package.