Does annotations order matter?

"Is there any impacts or benefits from a specific order ?" - Not as far as I know of, no. As was pointed out by Joop Eggen, the order of annotation processors or annotation scanners it the important part.


"What's the recommended order of annotations ?" - If one is able to identify a pattern of annotations commonly used together, I would recommend to group them in a new annotation and use the new annotation instead. One can achieve this by creating a new publice @interface MyAnnotation and annotate this new annotation with the annotations one wants to group. See this answer for an example.


Is there any impact or benefit from a specific order ?

Not that I am aware of. Remember how annotations work: some piece of code "looks" at your code, and checks for the presence of an annotation. Meaning: it "gets" an array of annotations, and checks whether the one it cares about is in that array. So order should be irrelevant.

Of course, when we talk about annotations that have compile-time effects, the story might be different. Such annotations have effects on the compilation process itself, so worst case, order of annotations changes the compilation process.

What's the recommended order of annotations?

The one that works for you. Meaning: sit down with your team, and ask yourself "do we prefer a specific order". If so, write that down, and have people follow that rule.

Real world example: we use annotations to "describe" the "properties" of our "objects". At some point, we saw that we often forgot annotation X when adding new properties. Because properties were written down in random order, thus hard to process manually (and we have plenty of different annotations, sometimes 5 to 10 on a single property).

Our "solution": annotations must be sorted alphabetically (when used for such "property" definitions). And we even have a unit test that checks for that sorting order. Since then: all "property" definitions follow the same rule. And that works nicely for us, because everybody comes with the same expectation and mindset.


In almost all cases the answer is No, the order has no effect.

But in fact it is a bit more complicated.

  1. Considering annotations that are processed by annotation processors, it was already stated in the other answers that it more depends on the order in which the processors run. However, the processors have access to the AST, which allows them to determine the order of annotations in the source code. So theoretically annotation processors can make the generated code dependent on the order, but I don't know any example for this and would call this bad practice.

  2. When getting the annotation of an element during runtime, you also have access to the order. The docs have more info on how the order is determined. So again, an implementation could make its behaviour dependent on the order. Again I would consider this bad practice. The only exception may be repeatable annotations, where I can think of use cases where this could be reasonable.

If there is any dependence on the order of annotations, which is very unlikely, this should be made very clear in the JavaDoc of the annotation.

So typically you are free to order them as you like. I don't know of any style guide on the order of annotations, so just make it reasonable for you.