Java Annotations

Lehane picture Lehane · Aug 23, 2008 · Viewed 33.3k times · Source

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time?

What are their typical usages?

Are they unique to Java? Is there a C++ equivalent?

Answer

Anders Sandvig picture Anders Sandvig · Aug 23, 2008

Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior. Frameworks such as JUnit and Hibernate use annotations to minimize the amount of code you need to write yourself to use the frameworks.

Oracle has a good explanation of the concept and its meaning in Java on their site.