<p>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?</p>

<p>What are their typical usages?</p>

<p>Are they unique to Java? Is there a C++ equivalent?</p>

[{'answer_id': 24227, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n  <p>Also, are they unique to Java, is there a C++ equivalent?</p>\n</blockquote>\n\n<p>No, but VB and C# have <a href="http://www.codeproject.com/KB/cs/dotnetattributes.aspx" rel="noreferrer">attributes</a> which are the same thing.</p>\n\n<p>Their use is quite diverse. One typical Java example, <code>@Override</code> has no effect on the code but it can be used by the compiler to generate a warning (or error) if the decorated method doesn\'t actually override another method. Similarly, methods can be marked obsolete.</p>\n\n<p>Then there\'s reflection. When you reflect a type of a class in your code, you can access the attributes and act according to the information found there. I don\'t know any examples in Java but in .NET this is used by the compiler to generate <a href="http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx" rel="noreferrer">(de)serialization</a> information for classes, determine the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx" rel="noreferrer">memory layout</a> of structures and <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx" rel="noreferrer">declare function imports</a> from legacy libraries (among others). They also control how the IDE form designer works.</p>\n\n<p>/EDIT: Attributes on classes are comparable to tag interfaces (like <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/io/Serializable.html" rel="noreferrer">Serializable</a> in Java). However, the .NET coding guidelines say not to use tag interfaces. Also, they only work on class level, not on method level.</p>\n'}, {'answer_id': 24231, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 7, 'selected': True, 'text': '<p>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 <a href="http://www.junit.org/" rel="noreferrer">JUnit</a> and <a href="http://www.hibernate.org/" rel="noreferrer">Hibernate</a> use annotations to minimize the amount of code you need to write yourself to use the frameworks.</p>\n\n<p>Oracle has <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html" rel="noreferrer">a good explanation of the concept and its meaning in Java</a> on their site.</p>\n'}, {'answer_id': 24335, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 4, 'selected': False, 'text': '<p>Anders gives a good summary, and here\'s an example of a JUnit annotation</p>\n\n<pre><code>@Test(expected=IOException.class)\npublic void flatfileMissing() throws IOException {\n    readFlatFile("testfiles"+separator+"flatfile_doesnotexist.dat");\n}\n</code></pre>\n\n<p>Here the <code>@Test</code> annotation is telling JUnit that the <code>flatfileMissing</code> method is a test that should be executed and that the expected result is a thrown <code>IOException</code>.  Thus, when you run your tests, this method will be called and the test will pass or fail based on whether an <code>IOException</code> is thrown.</p>\n'}, {'answer_id': 26408, 'author': 'pek', 'author_id': 2644, 'author_profile': 'https://Stackoverflow.com/users/2644', 'pm_score': 3, 'selected': False, 'text': '<p>Java also has the Annotation Processing Tool (apt) where not only you create annotations, but decide also how do these annotations work on the source code.</p>\n\n<p><a href="http://download.oracle.com/javase/1.5.0/docs/guide/apt/GettingStarted.html" rel="nofollow noreferrer">Here</a> is an introduction.</p>\n'}, {'answer_id': 73591, 'author': 'Scott Stanchfield', 'author_id': 12541, 'author_profile': 'https://Stackoverflow.com/users/12541', 'pm_score': 3, 'selected': False, 'text': '<p>To see some cool stuff you can do with Annotations, check out <a href="http://code.google.com/p/javadude/wiki/Annotations" rel="nofollow noreferrer">my JavaBean annotations and annotation processor</a>.</p>\n\n<p>They\'re great for generating code, adding extra validations during your build, and I\'ve also been using them for an error message framework (not yet published -- need to clear with the bosses...).</p>\n'}, {'answer_id': 20297975, 'author': 'Krishan', 'author_id': 449739, 'author_profile': 'https://Stackoverflow.com/users/449739', 'pm_score': 1, 'selected': False, 'text': '<p>By literal definition an annotation adds notes to an element. Likewise, Java annotations are tags that we insert into source code for providing more information about the code. Java annotations associate information with the annotated program element. Beside Java annotations Java programs have copious amounts of informal documentation that typically is contained within comments in the source code file. But, Java annotations are different from comments they annotate the program elements directly using annotation types to describe the form of the annotations. Java Annotations present the information in a standard and structured way so that it could be used amenably by processing tools.</p>\n'}, {'answer_id': 22934370, 'author': 'Koushik Balaji Venkatesan', 'author_id': 3472698, 'author_profile': 'https://Stackoverflow.com/users/3472698', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why">When do you use Java&#39;s @Override annotation and why?</a>\nThe link refers to a question on when one should use the override annotation(@override)..\nThis might help understand the concept of annotation better.Check out.</p>\n'}, {'answer_id': 34927561, 'author': 'Harvester', 'author_id': 5821907, 'author_profile': 'https://Stackoverflow.com/users/5821907', 'pm_score': 0, 'selected': False, 'text': "<p>Annotations when it comes to EJB is known as choosing Implicit middle-ware approach over an explicit middle-ware approach , when you use annotation you're customizing what you exactly need from the API\nfor example you need to call transaction method for a bank transfer : \nwithout using annotation :\nthe code will be   </p>\n\n<pre><code>transfer(Account account1, Account account2, long amount)    \n{\n   // 1: Call middleware API to perform a security check\n   // 2: Call middleware API to start a transaction\n   // 3: Call middleware API to load rows from the database\n   // 4: Subtract the balance from one account, add to the other\n   // 5: Call middleware API to store rows in the database\n   // 6: Call middleware API to end the transaction\n}\n</code></pre>\n\n<p>while using Annotation your code contains no cumbersome API calls to use the middle-\nware services. The code is clean and focused on business logic    </p>\n\n<pre><code>transfer(Account account1, Account account2, long amount) \n{\n   // 1: Subtract the balance from one account, add to the other\n}\n</code></pre>\n"}, {'answer_id': 71864556, 'author': 'Sean', 'author_id': 5790891, 'author_profile': 'https://Stackoverflow.com/users/5790891', 'pm_score': 2, 'selected': False, 'text': "<p>The first thing a newcomer to annotations will ask about annotations is: &quot;What is an annotation?&quot;  It turns out that there is no answer to this question, in the sense that there is <em>no common behavior</em> which is present in <em>all</em> of the various kinds of java annotations.  There is, in other words, nothing that binds them together into an abstract conceptual group other than the fact that they all start with an &quot;@&quot; symbol.</p>\n<p>For example, there is the @Override annotation, which tells the compiler to check that this member function overrides one in the parent class.  There is the @Target annotation, which is used to specify what kinds of objects a user defined annotation (a third type of construct with nothing in common with other kinds of annotation) can be attached to.  These have nothing to do with one another <em>except for starting with an @ symbol</em>.</p>\n<p>Basically, what appears to have happened is that some committee responsible for maintaining the java language definition is gatekeeping the addition of new keywords to the java language, and therefore other developers are doing an end run around that by calling new keywords &quot;annotations&quot;.  And that's why it is hard to understand, in general what an annotation is: because there is no common feature linking all annotations that could be used to put them in a conceptual group.  In other words, annotations as a concept do not exist.</p>\n<p>Therefore I would recommend studying the behavior of every different kind of annotation individually, and do not expect understanding one kind of annotation to tell you anything about the others.</p>\n<p>Many of the other answers to this question assume the user is asking about <em>user defined annotations</em> specifically, which are one kind of annotation that defines a set of integers or strings or other data, static to the class or method or variable they are attached to, that can be queried at compile time or run time.  Sadly, there is no marker that distinguishes this kind of annotation from other kinds like @interface that do different things.</p>\n"}]
['https://Stackoverflow.com/questions/24221', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/142/']