Introduction to Java Annotations

Annotations:
It provides information about program, and it is not part of program , so it won?t affect execution.
Uses of Annotations:
Basically annotations are used in many tools to know the information about program. Here I am going to list out few examples
  • Compiler uses annotations to detect errors and warnings.

  • Software tools uses annotation information to generate code, XML..etc

  • Some annotation are available to be examined runtime.

Java annotations also called as meta data. It is introduced in JDK 1.5 onwords. Before we write basic annotations, we need to understand about the following
  • How to create custom basic annotations

  • How to use annotations in Java Application


How to write custom Basic annotation:
Annotation is created by using @ sign followed by keyword interface.
Syntax:


Rules:
  • Annotation supports to declare using public and default.

  • Annotation methods can?t have parameters.

  • Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these.

  • Annotation methods can have default values.

  • Annotations by default extends with java.lang.annotation.Annotation interface.

  • Annotations can?t include any extend clause.

How to use annotations in Java Application:
  • Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements.

  • Each annotation often appears, by convention, on its own line.


Java supports two types of annotations
  • Regular Annotations:

  • Are applied to regular java classes, methods, statements.
  • Meta Annotations

  • Are applied to annotation definitions to describe the behavior of the annotation being declared and how it
    can be used.

Predefined Annotations in Java
@Deprecated, is used to tell the developer that a function
shouldn't be used anymore. It generally means that in a later
version of the code, that function will go away. When a developer does use this function, the compiler will warn the developer about this.
@Override tells the compiler that you intend to have a function override the parent's version of it. If there is a typo, the compiler knows there's a problem and will throw an error.
@Override also tells the developer that this code is overriding a parent's version. Easier to read than a non existent
comment to the same effect.
@SuppressWarnings@SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate.
In the following example, a deprecated method is used, and the compiler usually generates a warning. In this case, however,
the annotation causes the warning to be suppressed.
// use a deprecated method and tell 
   // compiler not to generate a warning
   @SuppressWarnings("deprecation")
    void useDeprecatedMethod() {
        // deprecation warning
        // - suppressed
        objectOne.deprecatedMethod();
    }
@SafeVarargs @SafeVarargs annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe
operations on its varargs parameter. When this annotation type is used, unchecked warnings relating to varargs usage are suppressed.
@FunctionalInterface @FunctionalInterface annotation, introduced in Java SE 8, indicates that the type declaration is intended to be
a functional interface, as defined by the Java Language Specification.
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment