Professional Documents
Culture Documents
Annotations
Annotations
Annotations
i. Classes
ii. Interfaces
iii. Exceptions
iv. Error
v. Enums
vi. Annotations
Annotations are not to develop the application just it is giving the metadata information to compiler.
@Override
@SuppressWarnings
@Deprecated
@FuntionalInterface
@SafeVararg
Meta annotation:
The meta annotations are giving information about general purpose annotations.
@Target
@Retention
@Documented
@Inherited
@Target : Indicates the contexts in which an annotation type is applicable.
CONSTRUCTOR
Constructor declaration
FIELD
LOCAL_VARIABLE
METHOD
Method declaration
PACKAGE
Package declaration
PARAMETER
TYPE
TYPE_PARAMETER
TYPE_USE
Use of a type
@Retention:
Indicates how long annotations with the annotated type are to be retained. If no Retention
annotation is present on an annotation type declaration, the retention policy defaults to
RetentionPolicy.CLASS
Annotations are to be recorded in the class file by the compiler but need not be
retained by the VM at run time. This is the default behavior.
public static final RetentionPolicy RUNTIME
Annotations are to be recorded in the class file by the compiler and retained by the
VM at run time, so they may be read reflectively.
@Documented:
Indicates that annotations with a type are to be documented by javadoc and similar tools by
default.
1.
@Target(value=METHOD)
@Retention(value=SOURCE)
{ @Override
2. @SuppressWarnings
@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(value=SOURCE)
}
public class Ex1 {
@SuppressWarnings("unchecked")
@SuppressWarnings("rawtypes")
objs.add(10);
objs.add(10.6);
objs.add("ratan");
3. @FunctionalInterface
@Documented
@Retention(value=RUNTIME)
@Target(value=TYPE)
@FunctionalInterface
interface Greeting
{ void greet();
4. @Deprecated
@Documented
@Retention(value=RUNTIME)
@Target(value={CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})
ex:
package com.stahya;
class Python
{ @Deprecated
void course()
python.course();
A program element annotated @Deprecated is one that programmers are discouraged from using,
typically because it is dangerous, or because a better alternative exists.
5. @SafeVarArg
@Documented
@Retention(value=RUNTIME)
@Target(value={CONSTRUCTOR,METHOD})