Annotations Flashcards
A form of metadata, provide data about a program that is not part of the program itself.
Annotations
What are the uses of annotations?
- Information for the compiler
- compile-time and deployment-time processing
- runtime processing
Annotations basics
- must start with @
- can include elements
- if there is only one element
@SuppressWarnings(“unchecked”) - if no elements
@Override - multiple annotations
- repeating annotations
Annotations can also be applied to the use of types
type annotation
Indicates that the marked element is deprecated and should no longer
be used.
@Deprecated
When an element is deprecated, it should also be documented using
the Javadoc
@deprecated
Informs the compiler that the element is meant to override an element
declared in a superclass.
Not required, but avoids errors (promotes proper overriding)
@Override
Annotation tells the compiler to suppress specific warnings that it
would otherwise generate
@SuppressWarnings
annotation specifies how the marked annotation is stored
@Retention
annotation indicates that whenever the specified
annotation is used those elements should be documented using the
Javadoc tool
@Documented
annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to
@Target
annotation indicates that the annotation type can be
inherited from the super class
@Inherited
annotation, indicates that the marked annotation can be applied more than once to the same declaration or type use
@Repeatable
can be used anywhere you use a type
- class instance creation expressions (new)
- Casts
- implements clauses
- throws clauses.
Type Annotation
ensures that your variable is not set to null
@NonNull