Sobes.tech
Junior — Middle

What types of @Retention annotations do you know and how do they differ?

sobes.tech AI

Answer from AI

In Java, the @Retention annotation determines how long the annotation will be retained and available at runtime. There are three main types of RetentionPolicy:

  1. SOURCE — the annotation is only available in the source code and is removed by the compiler. It does not appear in the compiled class file.

  2. CLASS — the annotation is retained in the compiled class but is not available at runtime via reflection. This is the default behavior.

  3. RUNTIME — the annotation is retained in the class and available at runtime via reflection. It is used for annotations that influence program behavior during execution.

Example:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value();
}
What types of @Retention annotations do you know and… - sobes.tech