JDK 5 provides annotations: the difference between Target, inherited, and documented _java

Source: Internet
Author: User

JDK 5 provides annotations, in addition to retention, there are another three, namely target, inherited and documented.

Target target, where the target annotation is used, defines the timing of the annotation, that is, the type of program element to which the annotation applies. If the Target meta annotation does not exist in the annotation type declaration, the declared type can be used on any program element. If such a meta annotation exists, the compiler enforces the specified usage limit.

Target is defined as follows:

@Documented
@Retention (retentionpolicy.runtime)
@Target (elementtype.annotation_type) public
@ Interface Target {
  elementtype[] value ();
}

As you can see, Target has only one value property, and the type is enumerated type ElementType. The ElementType statement reads as follows:

public enum ElementType {
  /** annotations can be used in classes, interfaces (including annotation types) or enum declaration/type
  ,
  /** field declarations (including enumeration constants)/Field
  ,
  /** METHOD declaration * *
  ,
  /** parameter declaration/
  PARAMETER,
  /** Construction Method declaration * * *
  constructor,
  /** local variable declaration
  / local_variable,
  /** Annotation type declaration * * *
  Annotation_type,
  /** Package declaration/
  PACKAGE
}

The documented annotation indicates whether the annotation information is added to the document when making Javadoc. If the annotation uses @documented when it is declared, the annotation information is added to Javadoc when you make Javadoc. The annotation statement is as follows:

@Documented
@Retention (value=runtime)
@Target (value=annotation_type) to indicate that the annotation can only be used when declaring annotations, that is, the meta annotation public
@ Interface documented {}

The inherited annotation is also a meta annotation and is declared as follows:

@Documented
@Retention (value=runtime)
@Target (value=annotation_type) public
@interface inherited {}

The inherited annotation indicates whether the annotation inherits from the quilt class, and the default is not inherited. When the annotation is declared, the @inherited annotation is used, and the annotation is inherited by subclasses of the class that used the annotation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.