Annotations


o Annotations provide additional information  about the code to the compiler or runtime  environment.

o Annotations are included in the source  code prefaced by the “@” symbol.

o JDK 1.5 includes three standard  annotation types: @Override , @Deprecated , and @SuppressWarnings

o The @Deprecated  annotation indicates  that a method has been deprecated.

o Works in conjunction with the @deprecated  JavaDoc tag that adds  instruction on which method to use  instead.

o Classes that call a deprecated method will  receive a warning at compile time.

o

o @Deprecated public void MyDeprecatedMethod()

o Annotations can include a single argument  or an array of arguments like this:

o @Annotation([name=]value[, …])

o @Annotation([name=]{value[, …]})

o Arguments can include an optional  parameter name.

o The @SuppressWarnings  annotation  turns off specified compiler warnings.

o Defines a single parameter called value .

@SuppressWarnings(value=“unchecked”)

public void nonGenericMethod()

//these formats are also supported

@SuppressWarnings(“unchecked”)

@SuppressWarnings(value={“unchecked”, “fallthrough”})

@SuppressWarnings({“unchecked”, “fallthrough”})

o Custom annotations can be created using the @interface declaration like this:

 

public @interface MyAnnotation {String value(); //defines a single parameter named value

}

@MyAnnotation(“argument value”)

public void myMethod()

o At runtime, custom annotations can be retrieved using reflection (if retention policy is set to RUNTIME).

About mayurmevada
I am a software professional with having helping nature...

Leave a comment