Java.lang Package Classes

Java Package - getPackage() Method



The java.lang.Package.getPackage() method is used to find a package by name in the callers ClassLoader instance. The callers ClassLoader instance is used to find the package instance corresponding to the named class. If the callers ClassLoader instance is null then the set of packages loaded by the system ClassLoader instance is searched to find the named package.

Packages have attributes for versions and specifications only if the class loader created the package instance with the appropriate attributes. Typically, those attributes are defined in the manifests that accompany the classes.

Syntax

public static Package getPackage(String name)

Parameters

name Specify a package name, for example, java.lang.

Return Value

Returns the package of the requested name. It may be null if no package information is available from the archive or codebase.

Exception

NA.

Example:

The example below shows how to use java.lang.Package.getPackage() method.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //create a java util package object
    Package p = Package.getPackage("java.util");

    //print the fully-qualified name of this package
    System.out.println(p.getName());
  }
}

The output of the above code will be:

java.util

❮ Java.lang - Package