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.

Syntax:

    public Package getPackage();

Parameter(s):

  • 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.

Example:

// 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: ");
        System.out.println(pack);
    }
}

Output

ArrayList defined in the: package java.util






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.