Java.lang.Package Class in Java (original) (raw)
Last Updated : 24 Apr, 2025
In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class.
**Note: The package class extends java.lang.Object and implements the **AnnotatedElement interface.
**Features of the Package Class:
- The package class provides details about the package, such as name, version, etc.
- The package class is part of **java.lang package, and because of this, we can use this in any program.
- We can get **package name, version, and other things with the help of methods like **getName(), **getSpecification(), and **getSpecificationVersion().
- We cannot directly create an object of the package class.
Declaration of Package in Java
We can declare the Package class like below:
public final class Package
**Note: The package class contains methods with the help of which we can easily get the information about packages in the Java environment.
Java Package Methods
Now, we are going to discuss each method one by one in detail:
**1. getAnnotation(Class annotationClass): Returns this element’s annotation for the specified type if such an annotation is present, else null.
**Syntax:
- **Parameter: This method takes one parameter **Class annotationClass, which specifies the type of annotation we want to retrieve from the target element
- **Return Type: Returnsthis element’s annotation for the specified annotation type if present on this element, else null.
**Example:
Java `
// Java program to demonstrates the // working of getAnnotation() method import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;
// declare a annotation type @Retention(RetentionPolicy.RUNTIME) @interface Demo { String str(); int val(); }
public class Geeks {
// setting values for the annotation
@Demo(str = " Gfg Demo Annotation", val = 100)
// a method to call in the main
public static void gfg() throws NoSuchMethodException
{
Geeks ob = new Geeks();
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("gfg");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
// checking the annotation
System.out.println(annotation.str() + " " + annotation.val());
}
public static void main(String args[]) throws Exception
{
gfg();
}
}
`
Output
Gfg Demo Annotation 100
**2. Annotation[] getAnnotations(): This method returns all annotations present on this element. (Returns an array of length zero if this element has no annotations.) The caller of this method is free to modify the returned array, it will have no effect on the arrays returned to other callers.
**Syntax:
public Annotation[] getAnnotations()
- **Parameter: This method does not take any parameter.
- **Return Type: This method returns an array of annotations present on this element.
**Example:
Java `
// Java program to demonstrates // the working of getAnnotation() method import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;
// declare a annotation type @Retention(RetentionPolicy.RUNTIME) @interface Demo { String str(); int val(); }
public class Geeks {
// setting values for the annotation
@Demo(str = " Gfg Demo Annotation", val = 100)
// a method to call in the main
public static void gfg() throws NoSuchMethodException
{
Geeks ob = new Geeks();
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("gfg");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
// checking the annotation
System.out.println(annotation.str() + " " + annotation.val());
Annotation[] gfg_ann = m.getAnnotations();
for(int i = 0; i < gfg_ann.length; i++)
{
System.out.println(gfg_ann[i]);
}
}
public static void main(String args[]) throws Exception
{
gfg();
}
}
`
Output
Gfg Demo Annotation 100 @Demo(str=" Gfg Demo Annotation", val=100)
**3. Annotation[] getDeclaredAnnotations(): This method returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array, it will have no effect on the arrays returned to other callers.
**Syntax:
public Annotation[] getDeclaredAnnotations()
- **Parameter: This method does not take any parameter.
- **Return Type: This method returns an array of annotation directly present on this element or return an empty array if none are present.
**Example:
Java `
// Java program to demonstrates the working of // getDeclaredAnnotation() method import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;
// declare a annotation type @Retention(RetentionPolicy.RUNTIME) @interface Demo { String str(); int val(); }
public class Geeks {
// setting values for the annotation
@Demo(str = " Gfg Demo Annotation", val = 100)
// a method to call in the main
public static void gfg() throws NoSuchMethodException
{
Geeks ob = new Geeks();
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("gfg");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
// checking the annotation
System.out.println(annotation.str() + " " + annotation.val());
Annotation[] gfg_ann = m.getDeclaredAnnotations();
for(int i = 0; i < gfg_ann.length; i++)
{
System.out.println(gfg_ann[i]);
}
}
public static void main(String args[]) throws Exception
{
gfg();
}
}
`
Output
Gfg Demo Annotation 100 @Demo(str=" Gfg Demo Annotation", val=100)
**4. String getImplementationTitle(): Return the title of this package.
**Syntax:
public String getImplementationTitle()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns the title of the implementation, null is returned if it is not known.
**5. String getImplementationVersion(): Return the version of this implementation. It consists of any string assigned by the vendor of this implementation and does not have any particular syntax specified or expected by the Java runtime. It may be compared for equality with other package version strings used for this implementation by this vendor for this package.
**Syntax:
public String getImplementationVersion()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns the version of the implementation, null is returned if it is not known.
**6. String getImplementationVendor(): Returns the name of the organization, vendor or company that provided this implementation.
**Syntax:
public String getImplementationVendor()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns the vendor that implemented this package.
**7. String getName(): Return the name of this package.
**Syntax:
public String getName()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns The fully-qualified name of the package.
**Example:
Java `
// Java code illustrating getName(), getImplementationTitle() // and getImplementationVendor() and getImplementationVersion() // methods class Geeks { public static void main(String arg[]) { Package pkgs[]; pkgs = Package.getPackages();
for(int i=0; i<1; i++)
{
// name of the package
System.out.println(pkgs[i].getName());
// checking title of this implementation
System.out.println(pkgs[i].getImplementationTitle());
// checking the vendor
System.out.println(pkgs[i].getImplementationVendor());
// version of this implementation
System.out.println(pkgs[i].getImplementationVersion());
}
}
}
`
**Output:
sun.reflect
Java Runtime Environment
Oracle Corporation
1.8.0_121
**8. static Package getPackage(String name): This method is used to find a package by its name using the ClassLoader instance
**Syntax:
public static Package getPackage(String name)
- **Parameter: This method takes single parameter name of the package to find
- **Return Type: Return the package of the requested name. It may be null if no package information is available from the archive or codebase.
**9. static Package[] getPackages(): This method gets all the packages currently known for the caller’s **ClassLoader instance. Those packages correspond to classes loaded via or accessible by name to that ClassLoader instance. If the caller’s ClassLoader instance is the bootstrap ClassLoader instance, which may be represented by null in some implementations, only packages corresponding to classes loaded by the bootstrap ClassLoader instance will be returned.
**Syntax:
public static Package[] getPackages()
- **Parameter: This method does not take any parameter.
- **Return Type: This method returns new array of packages known to the callers ClassLoader instance. An zero length array is returned if none are known.
**Example:
Java `
// Java program to demonstrates // the working of getPackages() method class Geeks { public static void main(String arg[]) { Package pkgs[]; pkgs = Package.getPackages();
Package pkg = Package.getPackage("java.lang");
for(int i=0; i<1; i++)
{
System.out.println(pkg.getName());
System.out.println(pkgs[i].getName());
}
}
}
`
Output
java.lang java.util.concurrent.locks
**10. String getSpecificationTitle(): Return the title of the specification that this package implements.
**Syntax:
public String getSpecificationTitle()
- **Parameter: This method does not take any parameter
- **Return Type: Return the specification title, null is returned if it is not known.
**11. String getSpecificationVersion(): Returns the version number of the specification that this package implements.
**Syntax:
public String getSpecificationVersion()
- **Parameter: This method does not take any parameter
- **Return Type: Return the specification version, null is returned if it is not known.
**12. String getSpecificationVendor(): Return the name of the organization, vendor, or company that owns and maintains the specification of the classes that implement this package.
**Syntax:
public String getSpecificationVendor()
- **Parameter: This method does not take any parameter
- **Return Type: Returns the specification vendor, null is returned if it is not known.
**13. int hashCode(): Return the hash code computed from the package name.
**Syntax:
public int hashCode()
- **Parameter: This method does not take any parameter
- **Return Type: Returns the hash code computed from the package name.
**Example:
Java `
// Java program to demonstrates the working // of hashCode(), getSpecificationTitle() // getSpecificationVendor() and getSpecificationVersion() class Geeks { public static void main(String arg[]) { Package pkgs[]; pkgs = Package.getPackages();
for(int i=0; i<1; i++)
{
// name of the package
System.out.println(pkgs[i].hashCode());
// checking title
System.out.println(pkgs[i].getSpecificationTitle());
// checking the vendor
System.out.println(pkgs[i].getSpecificationVendor());
// checking version
System.out.println(pkgs[i].getSpecificationVersion());
}
}
}
`
**Output:
685414683
Java Platform API Specification
Oracle Corporation
1.8
**14. boolean isCompatibleWith(String desired): Compare this package’s specification version with a desired version. It returns true if this packages specification version number is greater than or equal to the desired version number.
**Syntax:
public boolean isCompatibleWith(String desired)
- **Parameter: This method takes one parameter desired, which is the version string of the package to check compatibility with.
- **Return Type: Returns true if this package’s version number is greater than or equal to the desired version number
**15.boolean isSealed(): Returns true if this package is sealed.
**Syntax:
public boolean isSealed()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns true if the package is sealed, false otherwise.
**16. boolean isSealed(URL url): Returns true if this package is sealed with respect to the specified code source url.
**Syntax:
public boolean isSealed(URL url)
- **Parameter: This method takes one parameter url, which is the url of resource to check if the package is sealed.
- **Return Type: Returns true if the package is sealed, false otherwise.
**17. String toString(): Returns the string representation of this Package. Its value is the string “package ” and the package name. If the package title is defined it is appended. If the package version is defined it is appended.
**Syntax:
public String toString()
- **Parameter: This method does not take any parameter.
- **Return Type: Returns the string representation of the package.
**Example:
Java `
// Java program to demonstrates the // working of isCompatibleWith(), // toString(),isSealed methods import java.net.MalformedURLException; import java.net.URL;
class Geeks { public static void main(String arg[]) throws MalformedURLException { Package pkg = Package.getPackage("java.lang");
// checking if pkg is compatible with 1.0
System.out.println(pkg.isCompatibleWith("1.0"));
// checking if packet is sealed
System.out.println(pkg.isSealed());
URL url = new URL("https://www.youtube.com/");
System.out.println(pkg.isSealed(url));
// string equivalent of package
System.out.println(pkg.toString());
}
}
`
**Output:
true
false
false
package java.lang, Java Platform API Specification, version 1.8