Find the Package of an Object in Java

Description

The following code shows how to find the Package of an Object.

Example


//from   w w w  .jav a  2s  . c o m
import java.util.ArrayList;
import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    System.out.println(new Vector().getClass().getPackage().getName());
    System.out.println(new ArrayList().getClass().getPackage().getName());
    System.out.println("Test String".getClass().getPackage().getName());
    System.out.println(new Integer(1).getClass().getPackage().getName());
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy