Now for the weirdest part.
Annotations are not really something that will affect how you program in Java,
unless you need to associate some sort of metadata or annotations with classes, methods,
variables, etc.
So what are annotations anyway?
That's a good question. They provide a little extra information about the
classes you write, and a class can use the Reflection package later to read the
annotations. These are useful because you can attach extra information to your
code that may determine how it is used or maybe if it is used at all.
For example, in J2SE 5, you can declare your intent to override a method like toString() in one of your classes:
public class MyClass extends Object {
@Override
public String toString() {
return "My overridden method!";
}
}
In the above example, we declare
that we will override the immediately following toString() method. So the compiler looks in
our superclass (Object) for the same metho and makes
sure it exists. If for some reason we had overloaded toString() by declaring it with different
parameters and maybe return type, then the compiler would throw an error saying
we didn't override correctly. This is really useful if you want to make sure
you override a method as opposed to simply overloading it.
Of course you can define your own
annotations. They're basically like interfaces, but they can contain values. An
example annotation looks like:
public @interface Conference {
String what();
String when();
String location();
}
This annotation declares three
members: what,
when, location and
sets them up to have "getters" and "setters" automatically!
That means each @Conference annotation has those three fields
associated with it, and I don't have to define the accessor and mutator methods
to set them up (see the next code listing). If I define this annotation like
this, I can use it to mark code that I use for the Jett conference:
@Conference(what="JETT",
when="November 2004",
location="IUB")
public class MyMagicJettClass {
//...
}
And now the @Conference type of data is associated with
my class. Later on, I could write an analyzer that goes through all of my code
and lets me know which classes were used at conferences as well as which
conferences they were used at and when. This specific example doesn't have any
effect on the way MyMagicJettClass operates.
So the annotations require
two-fold cooperation: the programmer must properly annotate her code to provide
adequate metadata needed and other developers who will want to know this
metadata must know how to extract it using the Java Reflection package. That's
a whole hours-long session on how to extract them, so I'm not going to go into
depth here.
Where are they useful? Say you
are working with RMI (Remote Method Invocation) and you don't want all of your methods
available remotely. You could annotate the remotable ones with a @Remote annotation, then whatever serves
up the remote access can only allow those to be remotely accessed. There are a
ton of great uses for these, and they are fully extendable (you can annotate
annotations)!
No comments:
Post a Comment