Java › Annotations

How do you apply multiple different annotations to the same element, and does their order matter?

Beginner

Answer

Multiple distinct annotation types can simply be stacked on the same element, each on its own line or space-separated, and their order relative to each other has no semantic significance to the compiler or runtime (unlike modifiers like public static, which do have conventional ordering) -- tools reading them via reflection retrieve all applicable annotations regardless of the order they were written in the source.
@Override
@Deprecated
@SuppressWarnings("unchecked")
public List legacyMethod() {
    return new ArrayList();
}

// Equivalent, order doesn't matter:
@SuppressWarnings("unchecked") @Deprecated @Override
public List legacyMethod() { ... }
Real-world example A deprecated legacy method is marked with all three of @Override, @Deprecated, and @SuppressWarnings simultaneously, each serving its own independent purpose (compiler-checked override correctness, deprecation warning, and suppressing an unrelated raw-type warning) without any conflict or ordering requirement between them.

Common follow-ups: Are there any annotations where combining them together would actually cause a conflict or error?;How does an IDE typically choose to format/order stacked annotations by convention, even though it's not required?