Annotations provide data about a class that is not
part of the programming logic itself. Annotations are defined via
the @interface annotation before the class name.
· @Retention you define if the annotation
should be retained at runtime or not.
RetentionPolicy.SOURCE
RetentionPolicy.CLASS
RetentionPolicy.RUNTIME
· The @Target annotation lets you define where
this annotation can be used, e.g. the class, fields, methods, etc.
ElementType.ANNOTATION_TYPE
ElementType.CONSTRUCTOR
ElementType.FIELD
ElementType.LOCAL_VARIABLE
ElementType.METHOD
ElementType.PACKAGE
ElementType.PARAMETER
ElementType.TYPE
· The @Inherited annotation signals that a
custom annotation used in a class should be inherited by subclasses inheriting
from that class.
·The @Documented annotation is used to signal
to the JavaDoc tool that your custom annotation should be visible in the
JavaDoc for classes using your custom annotation
Java provides three built-in
annotations:
· @Override – When we want to override a
method of Super class, we should use this annotation to inform compiler that we
are overriding a method. So when super class method is removed or changed,
compiler will show error message. Learn why we should always use java override
annotation while overriding a method.
·@Deprecated –
when we want the compiler to know that a method is deprecated, we should use
this annotation. Java recommends that in javadoc, we should provide information
for why this method is deprecated and what is the alternative to use.
·@SuppressWarnings – This is just to tell compiler to ignore
specific warnings they produce, for example using raw types in java generics.
Its retention policy is SOURCE and it gets discarded by compiler.
No comments:
Post a Comment