The package is a namespace that is used to group logically related classes and interfaces.
Advantages/Benefits of the package in Java
- A package provides a unique namespace.
- A package provides access protection.
- A Package provides a grouping of logically related classes and interfaces that are easy to maintain.
How to create a package?
A package is created with a package keyword.
Java Package Example
package com.w3schools; public class PackageTest { public static void main(String args[]){ System.out.println("Package Test"); } }
Output
Package Test
Access the package from outside the package
1. Using import keyword:
import packagename.*;
All classes and interfaces will be accessible but not subpackages.
import packagename.ClassName;
Only specific class is accessible.
2. Using a fully qualified name.
Specific classes will be accessible and no need to import but everywhere it is used fully qualified name is needed.
Example of import packagename.*
package com.w3schools.display; public class Display { public void displayText(String text){ System.out.println(text); } }
package test; import com.w3schools.display.*; public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello W3chools360.com."); } }
Output
Hello W3chools360.com.
Example of import packagename.classname
package com.w3schools.display; public class Display { public void displayText(String text){ System.out.println(text); } }
package test; import com.w3schools.display.Display; public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello W3chools360.com"); } }
Output
Hello W3chools360.com
Example of using a fully qualified name
package com.w3schools.display; public class Display { public void displayText(String text){ System.out.println(text); } }
package test; public class Test { public static void main(String args[]){ com.w3schools.display.Display display = new display.Display(); display.displayText("Hello W3schools360.com!"); } }
Output
Hello W3schools360.com!
Subpackage
A package inside a package is known as a subpackage.
package com.w3schools.display; public class Display { public void displayText(String text){ System.out.println(text); } }
package com.w3schools.test; import com.w3schools.display.*; public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello W3schools360.com!"); } }
Output
Hello W3schools360.com!
Note: If a package is imported, all classes and interfaces will be accessible to that package but not to subpackages. So subpackage also be imported to access classes and interfaces of the subpackage.
package com.w3schools.display; public class Display { public void displayText(String text){ System.out.println(text); } }
package com.w3schools.test; import com.w3schools.*; public class Test { public static void main(String args[]){ //Error Display display = new Display(); display.displayText("Hello W3schools360!"); } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Display cannot be resolved to a type