What are Annotations in Java
π‘ Concept: Annotations
Annotations are metadata that provide additional information about Java code. They do not directly affect program logic but can be processed by the compiler or runtime to influence behavior.
π Quick Intro
Java annotations are used to provide supplementary information about code elements like classes, methods, or fields. They are prefixed with @ and can be processed at compile-time or runtime.
π§ Analogy
Think of annotations like labels on items in a warehouse. The label doesnβt change the item itself but provides extra information (e.g., "Handle with care").
π§ Technical Explanation
- Annotations are declared with the
@interface
keyword. - Common built-in annotations include
@Override
,@Deprecated
,@SuppressWarnings
. - Custom annotations can be created to add context or behavior to your code.
- Annotations can be processed by tools (e.g., compilers or frameworks like Spring).
π― Use Cases
- β Code documentation
- β Runtime configuration (e.g., Spring annotations)
- β Compile-time validation
- β JUnit test configuration
π» Example: Custom Annotation
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
String value() default "Test method";
}
public class TestExample {
@Test(value="Custom Test Method")
public void myTestMethod() {
System.out.println("Running test...");
}
public static void main(String[] args) {
try {
TestExample test = new TestExample();
for (java.lang.reflect.Method method : test.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(Test.class)) {
System.out.println("Annotation found: " + method.getAnnotation(Test.class).value());
method.invoke(test);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

β Interview Q&A
Q1: What are annotations in Java?
A: Annotations are metadata that provide additional information about code elements.
Q2: Can annotations affect the program's behavior?
A: No, they provide metadata and are processed by tools like compilers or frameworks to alter behavior.
Q3: What is the syntax for creating an annotation?
A: Use @interface
followed by the annotation name.
Q4: What is the purpose of the @Override
annotation?
A: It indicates that a method is overriding a method from its superclass.
Q5: How do you define a custom annotation?
A: By using @interface
and optionally specifying target elements and retention policy.
Q6: What does @Retention
specify?
A: It defines how long the annotation should be retained (e.g., source, class, runtime).
Q7: What does @Target
specify?
A: It defines where the annotation can be applied (e.g., method, field, class).
Q8: Can annotations be inherited?
A: Yes, if the @Inherited
annotation is used.
Q9: What are some built-in annotations in Java?
A: @Override
, @Deprecated
, @SuppressWarnings
.
Q10: How are annotations processed in Java?
A: Annotations can be processed at compile-time or runtime using reflection or annotation processors.
π MCQs
Q1. What is an annotation in Java?
- A type of variable
- Metadata that provides additional information about code elements
- A Java keyword
- A way to define classes
Q2. Which built-in annotation indicates a method is overriding a superclass method?
- @Override
- @SuppressWarnings
- @Deprecated
- @Test
Q3. What does @Retention specify?
- Where to apply the annotation
- How long an annotation is retained
- What the annotation does
- None of the above
Q4. What is the syntax to create a custom annotation?
- class
- interface
- @interface
- annotation
Q5. Can annotations affect the program’s behavior?
- Yes, always
- No, they provide metadata
- Sometimes
- Only with specific annotations
Q6. What is the purpose of @Target?
- Specifies annotation behavior
- Specifies where an annotation can be applied
- Defines annotation class
- Marks deprecated methods
Q7. Which annotation marks a method as deprecated?
- @Override
- @Deprecated
- @Test
- @SuppressWarnings
Q8. How do you define a custom annotation?
- By using @interface
- By using class
- By using enum
- By using annotation
Q9. Which built-in annotation helps in suppressing compiler warnings?
- @Override
- @Deprecated
- @SuppressWarnings
- @Test
Q10. How do annotations affect testing?
- By influencing code execution
- Annotations are not used in testing
- Annotations like @Test are used in frameworks like JUnit
- By suppressing errors
π‘ Bonus Insight
Annotations are widely used in frameworks like Spring, Hibernate, and JUnit to define configuration, behavior, and testing rules declaratively.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!