Predefined Annotations

A set of annotation types are predefined in the Java SE API. Some annotation types are used by the Java compiler, and some apply to other annotations.

Annotation Type Used by the Java Language

The predefined annotation types defined in java.lang are

  • @Deprecated,
  • @Override, and
  • @SuppressWarnings

@Deprecated

  • This annotation indicates that the marked element is deprecated and should no longer be used.
  • The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation.
  • When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example.
  • The use of the at sign (@) in both Javadoc comments and in annotations is not coincidental: they are related conceptually.
  • Also, note that the Javadoc tag starts with a lowercase d and the annotation starts with an uppercase D.
  • // Javadoc comment follows
    
    /**
     * @deprecated
     * explanation of why it was deprecated
     */
    @Deprecated
       static void deprecatedMethod() {}
    }
    

@Override

  • This annotation informs the compiler that the element is meant to override an element declared in a superclass.
  •  
    // mark method as a superclass method
    // that has been overridden
    @Override 
    int overriddenMethod() { }
    
  • While it is not required to use this annotation when overriding a method, it helps to prevent errors.
  • If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

@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();
    }
    
  • Every compiler warning belongs to a category.
  • The Java Language Specification lists two categories: deprecation and unchecked.
  • The unchecked warning can occur when interfacing with legacy code written before the advent of generics. To suppress multiple categories of warnings, use the following syntax:
  • @SuppressWarnings({"unchecked", "deprecation"})
    

@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.
 


Footer with Map