DEV Community

Cover image for Java Quickie. Annotations
Tristan Elliott
Tristan Elliott

Posted on • Edited on

Java Quickie. Annotations

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant.

  • All the information I used to create this post can be found HERE

Annotation Basics

  • The annotation that most of us are probably familiar with is @Override. The @ character indicates to the compiler that what follows is an annotation.
  • An annotation can also include elements, such as:
@Author(
  name="Bob"
  date=3/21/2021
)
Enter fullscreen mode Exit fullscreen mode
  • If the annotation has no elements then the parenthesis can be omitted, similar to @Override.

Where can Annotations be used?

  • Annotations can be applied to declarations: declarations of classes, fields, methods and other program elements. When used on declarations each annotation by convention appears on its own line.

Type Annotations

  • As of Java SE8 release, annotations can be applied to anywhere a type is used. These new and approved annotations are called type annotations. @Override and the annotation that we will be creating are examples of type annotations. Type annotations were created to support improved analysis of Java programs and improving stronger type checking.

Creating our own annotation

  • Say at the beginning of a class or method you have some comments describing it, like:
//Author: Bob
//Date: 3/17/2002
//Current version: 6
//Last modified: 4/12/2004
//By: Bob
Enter fullscreen mode Exit fullscreen mode
  • To add the same data with an annotation, you must first define the annotation type. The syntax for this is:
@interface CustomAnnotation {
           String author();
           String date();
           int currentRevision() default 1;
           String lastModified() default "N/A";
           String lastModifiedBy() default "N/A";
        }
Enter fullscreen mode Exit fullscreen mode
  • The annotation type definition looks similar to an interface definition where the keyword interface is placed after the @. Annotation types are actually a form of interface.
  • The body of the annotation definition contains annotation type element declarations, which sort of look like methods but they can define optional default values.
  • Once you have defined a annotation type like the one above, it can be used like so:
@CustomAnnotation(
    author = "Bob",
    date = "3/17/2002",
    currentRevision = 6,
    lastModified = "4/12/2004",
    lastModifiedBy = "Bob"
            )
    public void testingAnnotation() {

    }
Enter fullscreen mode Exit fullscreen mode
  • Now get out of here and create your own annotations!!

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)