Java Reflection - Java Class.getPackage()


Class.getPackage() has the following syntax.

public Package getPackage()


In the following code shows how to use Class.getPackage() method.

//w  w w  . j a va 2  m
public class Main {

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

    Class cls = Class.forName("java.lang.Integer");

    // returns the name and package of the class
    System.out.println("Class = " + cls.getName());
    System.out.println("Package = " + cls.getPackage());


The code above generates the following result.