Annotation Interview Questions

Annotations are nothing but meta-data added to the Java Elements. Annotations define a java type which is similar to classes, Enums, Interfaces and they can be applied to several Java Elements.
Annotations is a form of metadata which provides data about a program but itself is not part of the program. Annotations have no direct effect on the operation of the code they annotate. Annotations have a number of uses, of which some of them are as follows:
  • Information for the compiler — Annotations can be used by the compiler either to detect errors or suppress warnings.
  • Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files and so on.
  • Runtime processing — Some annotations are available to be examined at runtime.
  • 'Deprecated annotation' indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class or a field with the
  • 'Deprecated annotation'.
  • 'Override annotation' informs the compiler that the element is meant to override an element declared in a superclass.
  • 'SuppressWarnings annotation' tells the compiler to suppress specific warnings which would be generated otherwise.
  • 'SafeVarargs annotation', when applied to a method or a constructor, asserts that the code does not perform potentially unsafe operations on its 'varargsparameter'. When this annotation type is used, unchecked warnings relating to 'varargs' usage are suppressed.
  • 'FunctionalInterface annotation', introduced in Java SE 8, indicates that the type declaration is intended to be a 'functional interface', as defined by the 'Java Language Specification'.
Annotations were introduced with 5th version of Java.
Let's say for instance a software group traditionally starts the body of every class with comments providing important information:

public class Generation3List extends Generation2List {

// Author: Samle Author

// Date: 3/15/2012

// Current revision: 1

// Last modified: 6/12/2014

// By: Sample Author

// Reviewers: Tom Hilton

// class code goes here

To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:

@interface ClassPreamble {

String author();

String date();

int currentRevision() default 1;

String lastModified() default "N/A";

String lastModifiedBy() default "N/A";

// Note use of array

String[] reviewers();

The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces. The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values. After the annotation type is defined, you can use annotations of that type, with the values filled in, like this:

@ClassPreamble (

author = "Samle Author",

date = "3/15/2012",

currentRevision = 1,

lastModified = " 6/12/2014",

lastModifiedBy = "Sample Author",

// Note array notation

reviewers = {"Tom", "Hilton"}

  • 'Retention annotation' specifies how the marked annotation is stored.
  • 'Documented annotation' indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
  • 'Target annotation' marks another annotation to restrict what kind of Java elements the annotation can be applied to.
  • 'Inherited annotation' indicates that the annotation type can be inherited from the super class (though this is not true by default). When the user query's the annotation type and if the class has no annotation for this type, the class's superclass is queried for the annotation type. This annotation applies only to class declarations.
  • 'Repeatable annotation' that was introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same 'declaration' or 'type' in use. For more information, refer 'Repeating Annotations'.
Annotations that apply to other annotations are called "meta-annotations".
  • Entity
  • Table
  • Id
  • Column
  • Temporal
  • Basic
  • Enumerated
  • Access
  • Embeddable
  • Lob
  • AttributeOverride
  • Embedded
  • GeneratedValue
  • ElementCollection
  • JoinTable
  • JoinColumn
  • CollectionId
  • GenericGenerator
  • OneToOne
  • OneToMany
  • ManyToOne
  • ManyToMany
  • NotFound
  • Entity
  • Entity ( name="EMPLOYEES" )
    The first annotation will try to map the Class with the Table having the same name as Class whereas the second annotation will specify the Entity name as "EMPLOYEES" and hence will try to map with Table Name "EMPLOYEES".
  • Test
    The 'Test annotation' indicates that the public void method to which it is attached can be run as a test case.
  • Before
    The 'Before annotation' indicates that this method must be executed before each test in the class, so as to execute some pre-conditions necessary for the test.
  • BeforeClass
    The 'BeforeClass annotation' indicates that the static method to which it is attached must be executed once and before all tests in the class.
  • After
    The 'After annotation' indicates that this method gets executed after execution of each test.
  • AfterClass
    The 'AfterClass annotation' can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.
  • Ignores
    The 'Ignore annotation' can be used when you want temporarily disable the execution of a specific test.
Auto , Identity , Sequence and Table are some of the strategies that are used with the @GeneratedValue annotation.
Using an annotation Test with the argument gives us an expected exception.
  • Test (expected = Exception.class)
We can remove @Test from the respective test in order to avoid its execution. Alternatively we can put @Ignore annotation on the Junit file if we want to ignore all tests in a particular file.
We can either increase the test method's visibility and mark them with annotation @VisibleForTesting or we can use reflection to individually test those methods.
This is an informative annotation which specify that the interface is a functional interface. A 'Functional Interface' has only one 'abstract method' and many 'default methods'. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.
java.text' is the package name for an annotation class.
'Object' is said to be the parent class of annotation class.
Annotations that are once defined and used in some class, can be read using 'reflection package' methods like getAnnotations(). We have to first obtain the reference to the class which contains or uses the Annotaions and then we can write codeas shown below:

Class classObj = MyAnnotedClass.class;

Annotation[] annotations = classObj.getAnnotations();

for (Annotation annotation : annotations) {