« Sun Developer Day Review | Main | How not to comment code »

Annotation question answered

I've cleared up a question I didn't get answered at the Developer Day; the syntax for using annotations.

The examples given in the session were all like this:

public @Fred void someMethod() {}

But I don't like that syntax; if your annotations are long, it gets ugly. I prefer this one, which looks more like javadoc:


@Fred
public void someMethod() {}

Fortunately, you can do this; in fact, the examples in JSR175 are like this. It makes sense; the annotation is just another modifier, and Java lets you put modifiers in pretty much any order as long as they're before the return type.

Placing the annotations in the line above is an important aspect to laying out the code. Consider what you do with multiple annotations, or annotations that take lots of parameters, like this one from the JSR175 spec:


// Normal annotation
@RequestForEnhancement(
    id       = 2868724,
    synopsis = "Provide time-travel functionality",
    engineer = "Mr. Peabody",
    date     = "4/1/2004"
)
public static void travelThroughTime(Date destination) { ... }

Can you imagine squeezing that into the gap between public and static?

Post a comment


About

This page contains a single entry from the blog posted on June 8, 2004 11:37 PM.

The previous post in this blog was Sun Developer Day Review.

The next post in this blog is How not to comment code.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.35