views:

1047

answers:

5
+15  Q: 

Java Annotations

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 effect the running of a program??

What are typical usages for them?

Also, are they unique to Java, is there a C++ equivalent?

+4  A: 

Also, are they unique to Java, is there a C++ equivalent?

No, but VB and C# have attributes which are the same thing.

Their use is quite diverse. One typical Java example, @Override 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.

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 (de)serialization information for classes, determine the memory layout of structures and declare function imports from legacy libraries (among others). They also control how the IDE form designer works.

/EDIT: Attributes on classes are comparable to tag interfaces (like Serializable in Java). However, the .NET coding guidelines say not to use tag interfaces. Also, they only work on class level, not on method level.

Konrad Rudolph
+17  A: 

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.

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

Anders Sandvig
+3  A: 

Anders gives a good summary, and here's an example of a JUnit annotation

@Test(expected=IOException.class)
public void flatfileMissing()
    throws IOException
{
    readFlatFile("testfiles"+separator+"flatfile_doesnotexist.dat");
}

Here the @Test annotation is telling JUnit that the flatfileMissing method is a test that should be executed and that the expected result is a thrown IOException. Thus, when you run your tests, this method will be called and the test will pass or fail based on whether an IOException is thrown.

Eli Courtwright
+3  A: 

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.

Here is an introductory: http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html

pek
+1  A: 

To see some cool stuff you can do with Annotations, check out my JavaBean annotations and annotation processor at

http://code.google.com/p/javadude/wiki/Annotations

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...).

If you find the annotations I mention above useful, drop me a line at [email protected] -- Scott

Scott Stanchfield