Package:
Package is a namespace that is used to group logically related classes and interfaces.
Advantages/Benefits of package in java:
- Package provides unique namespace.
- Package provides access protection.
- Package provides grouping of logically related classes and interfaces so easy to maintain.
How to create a package:
A package is created with package keyword.
Example:
PackageExample1.java
package test; /** * This program is used to show simple use of package. * @author W3spoint */ public class PackageExample1 { public static void main(String args[]){ System.out.println("This is first package example."); } } |
Output:
This is first package example. |
How to access package from outside the package:
1. Using import keyword.
a. Import packagename.*;
All classes and interface will be accessible but not of subpackages.
b. Import packagename.classname;
Only specific class is accessible.
2. Using full qualified name.
Specific class will be accessible and no need to import but everywhere it is used fully qualified name is needed.
Example of import packagename.*
Display.java
package com.w3spoint.display; /** * This class is used to display entered text. * @author W3spoint */ public class Display { public void displayText(String text){ System.out.println(text); } } |
Test.java
package test; import display.*; /** * This class is used to show package use with import packagename.*. * @author W3spoint */ public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello java."); } } |
Output:
Hello java. |
Example of import packagename.classname
Display.java
package com.w3spoint.display; /** * This class is used to display entered text. * @author W3spoint */ public class Display { public void displayText(String text){ System.out.println(text); } } |
Test.java
package test; import display.Display; /** * This class is used to show package using import packagename.className. * @author W3spoint */ public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello java."); } } |
Output:
Hello java. |
Example of using full qualified name
Display.java
package display; /** * This class is used to display entered text. * @author W3spoint */ public class Display { public void displayText(String text){ System.out.println(text); } } |
Test.java
package test; /** * This class is used to show package using full qualified name. * @author W3spoint */ public class Test { public static void main(String args[]){ display.Display display = new display.Display(); display.displayText("Hello java."); } } |
Output:
Hello java. |
Subpackage:
A package inside a package is known as subpackage.
Example:
Display.java
package com.w3spoint.display; /** * This class is used to display entered text. * @author W3spoint */ public class Display { public void displayText(String text){ System.out.println(text); } } |
Test.java
package com.w3spoint.test; import com.w3spoint.display.*; /** * This class is used to show sub package using import. * @author W3spoint */ public class Test { public static void main(String args[]){ Display display = new Display(); display.displayText("Hello java."); } } |
Output:
Hello java. |
Download this example.
 Note: If a package is import, all classes and interface will be accessible of that package but not of subpackages. So subpackage also be import to access classes and interfaces of subpackage.
 Example:
Display.java
package com.w3spoint.display; /** * This class is used to display entered text. * @author W3spoint */ public class Display { public void displayText(String text){ System.out.println(text); } } |
Test.java
package com.w3spoint.test; import com.w3spoint.*; /** * This class is used to show sub package classes * will not import if package is imported. * @author W3spoint */ public class Test { public static void main(String args[]){ //Error Display display = new Display(); display.displayText("Hello java."); } } |
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Display cannot be resolved to a type Display cannot be resolved to a type at com.w3spoint.test.Test.main(Test.java:13) |
Download this example.
Next Topic: Access modifier in java with example.
Previous Topic: Constructor in java with example.