Home » Java programming language

Java Package getPackage() method with example

Package Class getPackage() method: Here, we are going to learn about the getPackage() method of Package Class with its syntax and example.
Submitted by Preeti Jain, on November 30, 2019

Package Class getPackage() method

  • getPackage() method is available in java.lang package.
  • getPackage() method is used to search a package by given package name in the caller's ClassLoader instance when the callers ClassLoader instance exists null then the bundles of package loaded by that System ClassLoader instance is searched to determine the named package.
  • getPackage() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • getPackage() method does not throw an exception at the time of returning package.

Syntax:

    public static Package getPackage(String pack_name);

Parameter(s):

  • String pack_name – represents the fully qualified name of the package.

Return value:

The return type of this method is Package, it returns the name of the given package else it returns null when no package exists from the database.

Example:

// Java program to demonstrate the example 
// of Package getPackage(String pack_name) 
// of Package method

public class GetPackage {
    public static void main(String[] args) {
        // Get Package by using getPackage() method
        Package pkg = Package.getPackage("java.util");

        // Get name of the package and
        //stored in a variable pname
        String pname = pkg.getName();

        // Display name of the package
        System.out.print("Package Name: ");
        System.out.print(pname);
    }
}

Output

Package Name: java.util



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.