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:

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:

public A getAnnotation(Class annotationClass)

**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()

**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()

**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()

**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()

**6. String getImplementationVendor(): Returns the name of the organization, vendor or company that provided this implementation.

**Syntax:

public String getImplementationVendor()

**7. String getName(): Return the name of this package.

**Syntax:

public String getName()

**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)

**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()

**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()

**11. String getSpecificationVersion(): Returns the version number of the specification that this package implements.

**Syntax:

public String getSpecificationVersion()

**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()

**13. int hashCode(): Return the hash code computed from the package name.

**Syntax:

public int hashCode()

**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)

**15.boolean isSealed(): Returns true if this package is sealed.

**Syntax:

public boolean isSealed()

**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)

**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()

**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