Java 1.5 Annotation Tutorial

Java 1.5 Annotation Tutorial thumbnail
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.

Related Searches:

References

  • Photo Credit Polka Dot RF/Polka Dot/Getty Images

Comments

You May Also Like

  • Java Frame Tutorials

    Java Frame Tutorials. The Frame classes in Java are 'Frame' in the AWT library and 'JFrame' in the SWING library, and are...

  • Java 1.5 Tutorial

    Java is an object oriented language, which means that programming in Java involves working with virtual objects. Java needs blueprints to create...

  • Java 1.5 Tutorials

    Java is a programming language you can use to build a variety of applications, whether you are doing database work, network-intensive tasks...

  • Java 1.4 Vs. Java 1.5

    Java 1.5, otherwise known as Java 5.0, is the next to latest release of the Java programming language and virtual machine, the...

  • Java 1.5 Enum Tutorial

    The enum type is a new feature in the Java 1.5.0 language, designed to solve weaknesses in the way enumerated values were...

  • Autocad Match Annotative Properties

    In computer drafting, annotations are the items used to add information to drawings. Annotations include text, dimensions and symbols with assigned properties...

  • Java 1.6 Vs. 1.5

    Java 1.6, also known as Java SE 6, has a plethora of new features compared to the older 1.5 edition. Most of...

  • How to Learn Core Java Online

    Java, a versatile computer programming language, can be used to run or write Java programs and applets (applets can be thought of...

  • How to Install a Java Compiler

    Java is a robust computer language that you can use in web design and standalone programs. A Java compiler is needed to...

  • Java Vs. Adobe

    Frequent computer users will be familiar with the names "Java" and "Adobe," but most do not understand what they are or what...

  • The Substring Method in Java

    The Java String class provides a variety of methods for accessing information about String content. The substring method returns a section of...

  • Java Projects on the Stock Market

    Java Projects on the Stock Market. Computer programs can help you analyze the stock market. Java is a popular interactive programming language...

  • How to Create a Website Using EJB

    Enterprise JavaBeans (EJBs) provide the infrastructure for building server-side Java components. EJBs are remote objects used for creation, deletion and invocation ...

  • MS Excel 2002 Tutorials

    MS Excel 2002 Tutorials. Microsoft Excel 2002 is the spreadsheet software program offered as part of the Microsoft 2002 XP Office Suite....

  • File Handling in Java Tutorial

    One of the features of the Java programming language is the large library of standard classes for solving routine programming tasks with...

  • How to Use Doxygen to Write Source Codes

    Keeping source code maintainable is one of the most difficult challenges facing any software developer, whether a lone wolf programmer or a...

  • How to Create Windows Help Documents

    Many Windows help documents have the ".CHM" extension since the older ".HLP" format is no longer supported by Windows Vista and future...

  • How to Send an Annotated Web Page with Diigo

    To annotate a Web page is to highlight text, pull out quotes, comment on images, draw on the page and otherwise mark...

  • How to Use Microsoft Live Meeting

    Microsoft Live Meeting is a special service offered as an add-on to the Microsoft Office package. It is a collaborative tool that...

  • Sun Java Tutorial

    Sun Java applications apply to a wide variety of contexts. Java programs built using the object oriented model are composed of distinct...

Related Ads

Featured