Java Class - getPackage() Method
The java.lang.Class.getPackage() method is used to get the package for this class. The class loader of this class is used to find the package. If the class was loaded by the bootstrap class loader the set of packages loaded from CLASSPATH is searched to find the package of the class. Null is returned if no package object was created by the class loader of this class.
Syntax
public Package getPackage()
Parameters
No parameter is required.
Return Value
Returns the package of the class, or null if no package information is available from the archive or codebase.
Exception
NA.
Example:
In the example below, the java.lang.Class.getPackage() method is used to get the package of the Vector class.
import java.lang.*; public class MyClass { public static void main(String[] args) { try { Class cls = Class.forName("java.util.Vector"); //print the name and package of the class System.out.println("Class name: " + cls.getName()); System.out.println("Package: " + cls.getPackage()); } catch (Exception e) { System.out.println(e); } } }
The output of the above code will be:
Class name: java.util.Vector Package: package java.util
❮ Java.lang - Class