Home » Java programming language

Java Class class getPackage() method with example

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

Class class getPackage() method

  • getPackage() method is available in java.lang package.
  • getPackage() method is used to return the package of this class, we find the package of the class by using class loader. Let suppose when the class was loaded by the bootstrap class loader the bundles of packages loaded from CLASSPATH are searched to determine the package of the class.
  • getPackage() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getPackage() method does not throw an exception at the time of returning an object of Package type.


    public Package getPackage();


  • It does not accept any parameter.

Return value:

The return type of this method is Package, it returns the following values based on the below given cases,

  • It returns the Package of the class which is loaded by the class loader of this Class.
  • It returns null, when no package is loaded by the class loader of this Class.


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

public class GetPackageOfClass {
    public static void main(String[] args) throws Exception {

        // Get Class object
        Class cl = Class.forName("java.util.ArrayList");

        // It return the package of the class ArrayList
        Package pack = cl.getPackage();

        // Display Package Name
        System.out.print("ArrayList defined in the: ");


ArrayList defined in the: package java.util

Comments and Discussions!

Load comments ↻

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