Java.lang Package Classes

Java Class - isAnnotation() Method



The java.lang.Class.isAnnotation() method returns true if this Class object represents an annotation type. Note that if this method returns true, isInterface() would also return true, as all annotation types are also interfaces.

Syntax

public boolean isAnnotation()

Parameters

No parameter is required.

Return Value

Returns true if this class object represents an annotation type; false otherwise.

Exception

NA.

Example:

In the example below, the java.lang.Class.isAnnotation() method is used to check if the given class object represents an annotation type or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //get a class object for this class
    MyClass x = new MyClass();
    Class xcls = x.getClass();

    //get a class object for Test
    Class ycls = Test.class;

    //check if x represents an annotation
    Boolean retval1 = xcls.isAnnotation();
    System.out.println("Is this class an annotation?: " + retval1);

    //check if x represents an annotation
    Boolean retval2 = ycls.isAnnotation();
    System.out.println("Is Test an annotation?: " + retval2);
  }
}

@interface Test {
  //code block
}

The output of the above code will be:

Is this class an annotation?: false
Is Test an annotation?: true

❮ Java.lang - Class