Close

Java Reflection - Class.getPackage() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Package getPackage()
Returns:
the package of the class, or null if no package information is available from the archive or codebase.

Examples


package com.logicbig.example.clazz;

import java.util.Collection;

public class GetPackageExample {

public static void main(String... args) {
Class<GetPackageExample> c = GetPackageExample.class;
Package p = c.getPackage();
System.out.println(p.getName());

Class<Collection> c2 = Collection.class;
Package p2 = c2.getPackage();
System.out.println(p2.getName());
}
}

Output

com.logicbig.example.clazz
java.util




package com.logicbig.example.clazz;

public class GetPackageExample2 {

public static void main(String... args) {
Class<Integer> c = int.class;
Package p = c.getPackage();
System.out.println(p);

Class<int[]> c2 = int[].class;
Package p2 = c2.getPackage();
System.out.println(p2);

Class<Void> c3 = void.class;
Package p3 = c3.getPackage();
System.out.println(p3);

Class<Void> c4 = Void.class;
Package p4 = c4.getPackage();
System.out.println(p4);
System.out.println(p4.getName());

}
}

Output

null
null
null
package java.lang
java.lang




See Also