Java 1.5 Annotation Tutorial
Customizable annotations are a feature added to the Java programming language as of version 1.5. Unlike most programming features in the Java language, they have no direct effect on the operation of the code. However, they can serve a number of purposes. For example, annotations can provide metadata for other developers, similar to the tags used in JavaDoc comments. They can also make data available to the compiler that can help programmers catch easily made errors that the compiler would not normally notice.
Instructions
-
-
1
Create a new Java class file named "AnnotationTest.java" and paste the following code into it:
class AnnotationTest extends JFrame {
public udpate() {
// Some code here.
}
}As you can see, this simple class extends the JFrame class built into the Java SWING library. However, it appears the developer has made a typing error. While he obviously intended to override the update() method from the superclass, he misspelled "update." Normally, the developer might not catch the error until hours later when some aspect of his program, possibly in a completely different class, fails to work properly. While there is certain to be problems as a result of this mistake, depending on the context of the mistake, there might not even be compiler error, since any code elsewhere in the program can always fall back upon the "update" method in the JFrame class. Wouldn't it be nice if there was a way to tell the compiler that this method is intending to override another method? Enter the @Override annotation.
-
2
Edit your code so that it reads as follows:
@Override
public udpate() {
// Some code here.
}The @Override annotation gives the compiler a glimpse of what is in the programmer's head. In this case, that the programmer intends his method "udpate" to override some other method in the superclass. Now, when the compiler reads this program, it will realize immediately that a mistake has been made. The programmer claims "udpate" overrides another method, but the class JFrame contains no method called "udpate." The compilation will fail and the compiler will let the programmer know explicitly that his "udpate" method is not doing what he expected.
Hopefully, the programmer will take a quick look and notice his oversight, and correct his misspelling.
-
-
3
Put the word "@Deprecated" just after the "@Override" annotation for your method. If you have been programming in Java long, you probably recognize @Deprecated as a JavaDoc tag that indicates to other developers, when they read the Javadoc documentation, that a class or method is no longer supported and that they should stop using it as soon as possible.
Of course, in practice, few developers are compulsively studying the JavaDocs for all the libraries they use with each new release. So, instead of counting on the Java developers to take the initiative, the @Deprecated annotation tells the compiler to generate a warning anytime the class or method is used. That way, anytime a developer builds a new version of her program, she is told immediately that she is using a method or class that is now considered defunct and obsolete.
-
1
References
- Photo Credit Polka Dot RF/Polka Dot/Getty Images