In this tutorial, we will learn what annotations are, different Java annotations, types of Java Annotations and how to use them with the help of examples.
Java Annotations allow us to add metadata information into our source code, although they are not a part of the program itself. Annotations were added to the java from JDK 5. Annotation has no direct effect on the operation of the code they annotate (i.e. it does not affect the execution of the program).
An annotation always starts with the symbol @ followed by the annotation name. The symbol @ indicates to the compiler that this is an annotation.
For e.g. @Override Here @ symbol represents that this is an annotation and the Override is the name of this annotation.
Annotations can be applied to the classes, interfaces, methods and fields. For example the below annotation is being applied to the method.
@Override
void myMethod()
{
//Do something
}
Example :
@AnnotationDemo()
Example :
@AnnotationDemo("testing")
Example :
@AnnotationDemo(element1 = "value1", element2 = "value2")
Example :
Constructor invocations
new @Readonly HashSet<>()
Type definitions
@NonNull String s;
Type casts
newStr = (@NonNull String) s;
extends and implements clause
class Warning extends @Localized Message
throws clause
public String readMethod() throws @Localized IOException
class Codemistic
{
/*
-> @deprecated
-> This method is deprecated and has been replaced by newMethod()
*/
@Deprecated
public static void deprecatedMethod()
{
System.out.println("Deprecated method");
}
public static void main(String args[])
{
deprecatedMethod();
}
}
Output :
Deprecated method
class Base
{
public void Display()
{
System.out.println("Base display()");
}
}
class Derived extends Base
{
@Override
public void Display()
{
System.out.println("Derived display()");
}
}
class Main
{
public static void main(String args[])
{
Base t1 = new Derived();
t1.Display();
}
}
Output :
Derived display()
class DeprecationTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecationtest display()");
}
}
public class SuppressWarningsTest
{
// If we comment below annotation, program generates warning
@SuppressWarnings({"checked", "deprecation"})
public static void main(String args[])
{
DeprecationTest d1 = new DeprecationTest();
d1.Display();
}
}
Output :
Deprecationtest display()
import java.util.ArrayList;
import java.util.List;
public class JavaExample
{
@SafeVarargs
private void print(List... names)
{
for (List<String> name : names)
{
System.out.println(name);
}
}
public static void main(String[] args)
{
JavaExample obj = new JavaExample();
List<String>list = new ArrayList<String>();
list.add("Adarsh");
list.add("John");
list.add("Megan");
obj.print(list);
}
}
Output :
Adarsh
John
Megan
@FunctionalInterface
interface Square
{
int calculate(int x);
}
@Retention(RetentionPolicy)
Types of Retention Policy
@Documented
@Target(ElementType)
Target Constant | Annotations Can be Applied To |
ElementType.ANNOTATION_TYPE | Another annotation |
ElementType.CONSTRUCTOR | Constructor |
ElementType.FIELD | Field |
ElementType.LOCAL_VARIABLE | Local variable |
ElementType.METHOD | Method |
ElementType.PACKAGE | Package |
ElementType.PARAMETER | Parameter |
ElementType.TYPE | Class, Interface, or enumeration |
@Inherited
import java.lang.annotation.Repeatable;
@Repeatable(Cities.class)
public @interface City
{
String name();
}
public @interface Cities
{
City[] value();
}
[Access Specifier] @interface<AnnotationName>
{
DataType <Method_Name>() [default value];
}
Example :
@interface MyCustomAnnotation
{
String value() default "default value";
}
class Test
{
@MyCustomAnnotation(value = "codemistic")
public void method1()
{
System.out.println("Test method 1");
}
public static void main(String[] args) throws Exception
{
Test obj = new Test();
obj.method1();
}
}
Output
Test Method 1