Search code examples
javaannotationscode-formattingconventions

Are there any conventions for how to order Java annotations?


For projects heavily used of Java annotations, is there any suggestion on the annotation order? So as to uniform the code style in the team.


Solution

  • I'm going to say there's not. I could be proven wrong on this.

    My reasoning is there are hundreds of projects that have annotations, in addition to the ones baked into Java, and the ones baked into our IDEs (specifically Eclipse but I'm sure others have them too.)

    So, with all these libraries potentially competitng for "whose on top" I doubt they would agree on a standard.

    I would encourage your team to sit down and make decisions about what's best for you guys.

    Factors I would consider:

    1. Which annotations are most important? They should be near the top.
    2. Which annotations are most likely to be indicative of potential bugs? They should be near the top.
    3. Which annotations are likely to be on every method in the class? They should be near the bottom.