Packages
Package in java is nothing but a kind of directory which contains a group of similar types of classes, interfaces and subpackages. Packages can be created by right click on the src folder and select option package and give it a name.
You need to use the keyword package in the program to access the components in that package.Packages can be categorized in 2 categories.
- In-built packages
- user defined packages
In-built packages are like java, lang, awt, javax, swing, util, sql, io ,net etc.
User defined packages can be like any desired name which suits the requirement.In our example ,we have create a packages called com.testingpol.demo.(as shown below)
package com.testingpol.demo; public class AddNumber { public static void main(String[] args) { System.out.println("Package keyword is used above."); } }
Advantages of packages:
- Packages can be used to maintain the similar kind of classes in one directory.
- Remove naming collision.
- Provides access protection.
Subpackages : It will be created inside a package to further clarify it. In our example, we have full package name com.testingpool.demo. In that, testingpool and demo are the subpackages of com.
The import keyword is used to access the package from outside. For showing this example, we have created 2 packages com.testingpool.demo and com.testingpool.demo2. A class AddNum is created under the package com.testingpool.demo and another class SubNumber is created under another pacage com.testingpool.demo2.
Now, if we want to create object of class AddNumber in the second package com.testingpool.demo2.We have to import the first package inside the second package.Look at the below example for better understanding.
Class AddNumber under the package com.testingpool.demo.
package com.testingpol.demo; public class AddNumber { public static void main(String[] args) { System.out.println("Package keyword is used above."); } }
Class SubNumber under the package com.testingpool.demo2.
package com.testingpool.demo2; import com.testingpol.demo.AddNumber; // importing the package to use its class public class SubNumber { public static void main(String[] args) { AddNumber num = new AddNumber(); } }
There are 3 ways to access the package into another class or package.
- By packagename.*
- By package.classname
- By using fully package name
Using package.*:
package com.testingpool.demo2; import com.testingpol.demo.*; // using package.* public class SubNumber { public static void main(String[] args) { AddNumber num = new AddNumber(); } }
Using Package.classname:
AddNumber is the class under the package com.testinpool.demo.
package com.testingpool.demo2; import com.testingpol.demo.AddNumber;; // using package.classname public class SubNumber { public static void main(String[] args) { AddNumber num = new AddNumber(); } }
using full package name:
Instead of importing the package, use the full package name to use its components as shown below.
package com.testingpool.demo2; public class SubNumber { public static void main(String[] args) { //use full package name com.testingpol.demo.AddNumber num = new com.testingpol.demo.AddNumber(); } }