Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Show how to declare a repeatable annotation @Schedule(cron = "...") so a method can carry it twice, and how to read all of them reflectively.

We mark @Schedule with @Repeatable(Schedules.class) and declare a container annotation @Schedules whose value() returns Schedule[], with compatible retention and target. When a method carries @Schedule twice, the compiler stores both inside one @Schedules, so we read them with method.getAnnotationsByType(Schedule.class), which looks through the container. getAnnotation(Schedule.class) returns null in that case.

Why this question is asked

Before Java 8, a method could carry each annotation only once, so libraries that needed several values invented wrapper annotations that users had to write by hand. Repeatable annotations made that wrapper automatic, and Spring's own @Scheduled uses the feature. Interviewers ask this coding question because getting it slightly wrong produces a bug that is hard to spot: the annotations are there, but the reading code does not see them. In this answer, we will recap annotations and retention, declare @Schedule and its container, use it twice on a method, read the values reflectively, and see exactly why getAnnotation returns null.

1A Quick Recap of Annotations and Retention

An annotation is a piece of metadata we attach to a class, method, field or other element. It does nothing by itself; some other code, a compiler plugin or a framework, reads it and acts on it. Each annotation type declares two important things with meta-annotations, which are annotations placed on the annotation type itself.

@Target says where the annotation may be used, for example ElementType.METHOD. @Retention says how long it is kept: SOURCE annotations are thrown away by the compiler, CLASS annotations are written to the class file but not visible to reflection, and RUNTIME annotations can be read with reflection while the program runs. Our scheduler reads the cron expressions at run time, so @Schedule needs RUNTIME retention.

2Declaring @Schedule and Its Container

A repeatable annotation needs two declarations. The annotation itself is marked with @Repeatable, which names the container type. The container is an ordinary annotation whose value() method returns an array of the repeatable one.

Java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Repeatable(Schedules.class) // the compiler will use Schedules to hold several
public @interface Schedule {
String cron();
}
@Retention(RetentionPolicy.RUNTIME) // at least as long as Schedule's retention
@Target(ElementType.METHOD) // no element kinds that Schedule does not allow
public @interface Schedules {
Schedule[] value();
}

The compiler checks that the two fit together. The container's retention must be the same as or longer than the repeatable annotation's; if Schedules were CLASS while Schedule is RUNTIME, the container would vanish at run time with the values inside it, so javac rejects it. The container may only be applicable to element kinds that Schedule also allows. And if Schedule is marked @Documented or @Inherited, the container must be marked the same way, so that both behave alike in Javadoc and in inheritance. Any other methods in the container must have default values.

3Using the Annotation Twice

With both types declared, we can put @Schedule on a method as many times as we like.

Java
public class Reports {
@Schedule(cron = "0 0 6 * * MON-FRI")
@Schedule(cron = "0 0 9 * * SAT")
public void sendDailyReport() { }
@Schedule(cron = "0 30 23 * * *")
public void archive() { }
}
What the compiler stores on each method
MethodWritten in sourceStored in the class file
sendDailyReport()Two @Schedule annotationsOne @Schedules holding two @Schedule
archive()One @Schedule annotationOne plain @Schedule, no container
The container only appears when the annotation is repeated.

Here is the part that matters for reading them later. The class file does not contain two @Schedule annotations on sendDailyReport(). The compiler rewrites them into a single container, exactly as if we had written @Schedules({@Schedule(cron = "0 0 6 * * MON-FRI"), @Schedule(cron = "0 0 9 * * SAT")}). On archive(), where there is only one, the compiler leaves a plain @Schedule with no container.

4Reading All Schedules Reflectively

The reflection API added getAnnotationsByType in Java 8 for exactly this. It returns the annotations of the requested type whether they are present directly or wrapped in their container.

Java
Method daily = Reports.class.getMethod("sendDailyReport");
Schedule[] all = daily.getAnnotationsByType(Schedule.class);
System.out.println(all.length); // 2
for (Schedule s : all) System.out.println(s.cron()); // 0 0 6 * * MON-FRI
// 0 0 9 * * SAT
Method archive = Reports.class.getMethod("archive");
System.out.println(archive.getAnnotationsByType(Schedule.class).length); // 1
System.out.println(archive.getAnnotationsByType(Schedule.class)[0].cron()); // 0 30 23 * * *

Notice that the same call works for one annotation and for two, so the scheduler does not need two code paths. There is also getDeclaredAnnotationsByType, which ignores annotations inherited from a superclass; for methods the two return the same result, because method annotations are never inherited, but for classes marked with an @Inherited annotation they can differ.

5Why getAnnotation(Schedule.class) Returns null

The older method getAnnotation looks only for an annotation of exactly the requested type, directly on the element. It does not know about containers.

Java
System.out.println(daily.getAnnotation(Schedule.class)); // null
System.out.println(daily.getAnnotation(Schedules.class).value().length); // 2
System.out.println(archive.getAnnotation(Schedule.class).cron()); // 0 30 23 * * *
System.out.println(archive.getAnnotation(Schedules.class)); // null

On sendDailyReport() the only annotation present is @Schedules, so asking for @Schedule finds nothing. On archive() it is the other way round. So code that uses getAnnotation(Schedule.class) works perfectly in tests with one schedule and silently drops every schedule the day someone adds a second one. That is the reason library code that supports repeatable annotations should always use the ByType methods.

6Follow-up questions

What does getAnnotation(Schedules.class) return when only one @Schedule is present?

It returns null. The compiler only creates the container when the annotation is repeated; a single @Schedule is stored as itself. That is why reading the container directly is not enough, and why getAnnotationsByType, which handles both cases, is the right call.

Can we write the container by hand instead of repeating the annotation?

Yes. Writing @Schedules({@Schedule(cron = "a"), @Schedule(cron = "b")}) produces the same class file as two @Schedule annotations, and getAnnotationsByType(Schedule.class) returns both. This was the only way before Java 8, and it still compiles.

How does Spring read its own repeatable @Scheduled?

Spring's @Scheduled is repeatable with the @Schedules container, and Spring reads it through its own annotation utilities, which look through containers and also through meta-annotations, annotations placed on other annotations. Plain reflection's getAnnotationsByType does not search meta-annotations, which is one reason frameworks build their own helpers.

7Conclusion

In this answer, we declared a repeatable @Schedule with @Repeatable(Schedules.class) and a container @Schedules holding a Schedule[] value, with retention at least as long as @Schedule's, a compatible target and matching @Documented and @Inherited settings. We saw that the compiler wraps repeated annotations in the container but leaves a single one alone, so getAnnotationsByType(Schedule.class) is the reliable way to read them, and getAnnotation(Schedule.class) returns null as soon as there are two.

What to say in the interviewAbout 70 seconds aloud

Open with

To make an annotation repeatable I need two types. The annotation itself, Schedule, with a cron attribute, RUNTIME retention, a METHOD target, and the meta-annotation Repeatable pointing at the container.

Then, if they want more

The container, Schedules, has a single value method returning Schedule array. The compiler checks they are compatible: the container's retention must be at least as long, it can only target element kinds Schedule allows, and Documented and Inherited must match. When a method has two Schedule annotations, the compiler actually stores one Schedules annotation that contains both.

With a single Schedule, it stores just that one, with no container. So to read them I call method.getAnnotationsByType(Schedule.class), which returns both in the first case and one in the second, and I loop over them to get the cron expressions. The trap is getAnnotation(Schedule.class): with two annotations it returns null, because the only thing directly present is the container.

And the reverse is also true: getAnnotation(Schedules.class) returns null when there is only one. So any library code that reads repeatable annotations should use the ByType methods.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Show how to declare a repeatable annotation @Schedule(cron = "...") so a method can...": https://scrivio-interview-library.netlify.app/java/annotations/8b69bd88a9/