Showing posts with label Java Package Class. Show all posts
Showing posts with label Java Package Class. Show all posts

Tuesday, 8 September 2015

Java Tutorial : Java package Class

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

What 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(): Returns true if the package is sealed, or false otherwise.

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());
  }
}

Watch More Java Tutorials:

Tutorials