Master Core Java Concepts!
Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!
Subscribe Now on YouTubeWhat Is the Package Class in Java?
In Java, the Package class belongs to the java.lang package. It provides methods to retrieve version, specification, and implementation details about a specific Java package at runtime.
To get a Package object, you can call the static Package.getPackage("packagename") method—for example, Package.getPackage("java.util")—and then inspect its metadata.
Commonly Used Methods of the Package Class
getName(): Returns the name of the package (e.g.,java.util).getSpecificationTitle(): Returns the title of the specification that this package implements.getSpecificationVendor(): Returns the name of the organization or vendor that owns and maintains the specification.getSpecificationVersion(): Returns the version number of the specification.getImplementationTitle(): Returns the title of this package's implementation (e.g.,Java Runtime Environment).getImplementationVendor(): Returns the vendor that provided the implementation.getImplementationVersion(): Returns the version of this implementation.isSealed(): Returnstrueif the package is sealed, orfalseotherwise.
Code Example: Getting Package Information
public class PackageInfoDemo {
public static void main(String[] args) {
Package pkg = Package.getPackage("java.util");
System.out.println("Package Name: " + pkg.getName());
System.out.println("Specification Title: " + pkg.getSpecificationTitle());
System.out.println("Specification Vendor: " + pkg.getSpecificationVendor());
System.out.println("Specification Version: " + pkg.getSpecificationVersion());
System.out.println("Implementation Title: " + pkg.getImplementationTitle());
System.out.println("Implementation Vendor: " + pkg.getImplementationVendor());
System.out.println("Implementation Version: " + pkg.getImplementationVersion());
System.out.println("Is Sealed: " + pkg.isSealed());
}
}
public static void main(String[] args) {
Package pkg = Package.getPackage("java.util");
System.out.println("Package Name: " + pkg.getName());
System.out.println("Specification Title: " + pkg.getSpecificationTitle());
System.out.println("Specification Vendor: " + pkg.getSpecificationVendor());
System.out.println("Specification Version: " + pkg.getSpecificationVersion());
System.out.println("Implementation Title: " + pkg.getImplementationTitle());
System.out.println("Implementation Vendor: " + pkg.getImplementationVendor());
System.out.println("Implementation Version: " + pkg.getImplementationVersion());
System.out.println("Is Sealed: " + pkg.isSealed());
}
}