Introduction to Annotations in Java
In Java 5.0, a new feature called annotations was
added to the Java to make it easier to create new kinds of metadata for Java
programs.
- Annotations are not mere comments; they can help us in different ways.
- Annotations, decorates programs with additional information, they are not the part of program itself.
- Annotations don't directly affect program semantics but provides general purpose metadata.
- Annotations can affect the treatment of programs by the software tools and libraries.
- Annotations can be used by the compiler to detect errors and suppress warnings.
- Annotations can be used by the software tools to generate the code like XML files.
- Few Annotations can be examined at run-time also.
- Annotations encourage declarative programming style.
- Annotations simplify several programming tasks and help a great deal to develop Web Services and Enterprise application development like EJBs.
- Annotations are also used for tasks like Testing Frameworks and Parser Generators etc.
In its basic form, an annotation will look like:
@TestAnnotation
The @ character indicates to the compiler that what follows
is an annotation. In the following example, the annotation's name is Override:
@Override
void method() { ... }
There are several Built-in Annotations defined Java but nine
are general purpose Annotations.
Built-in Annotations
included in java.lang
- @Override
- @Deprecated
- @SuppressWarnings
- @FunctionalInterface
- @SafeVarargs
Built-in Annotations used by other Annotations internally,
included in java.lang.Annotation
- @Target
- @Retention
- @Inherited
- @Documented
@Override
@Override is a standard marker annotation that can be
used to annotate method definitions. It means that the method is intended to
override (that is replace) a method with the same signature that was defined in
some super class. A compiler can check that the super class method actually
exists; if not, it can inform the programmer.
Example:
Example:
class A
{
public void testMethod()
{
System.out.println("somthong in super class");
}
}
public class B extends A{
@Override
public void testMethod()// compile time error will be reported if method does not
{ //exist in super class of A
System.out.println("something in sub class");
}
}
@Deprecated
@Deprecated, is a marker Annotation and indicates this
declaration is obsolete and has been replaced by newer implementation.
@SuppressWarnings
This Annotation indicates that one or more warnings might be
issued by the compiler are needed to be surprised. The warning to be suppressed
are specified by the name in string format.
@FunctionalInterface
@FunctionalInterface is a marker annotation which was added
in JDK 8. This Annotation was designed to be used with interfaces only. It
indicates that the annotated interface is a functional interface. A functional
interface is an interface containing one and only one abstract method.
Functional interfaces are used by Lambda expressions. If the annotated
interface is not a functional interface a compile time error will be reported.
@FunctionalInterface is purely informational.
@SafeVarargs
This marker annotation can be applied to the methods and the
constructors. This annotation specifies that no unsafe acts related to
varargs parameter occur. This annotation
must be applied only to the the varargs method constructors those are static or
final.