Java Annotations

braindead
Dev Genius
Published in
2 min readOct 21, 2022

--

Java Annotation is a tag that represents metadata, i.e. attached to a class, interface, method, or field to indicate some additional information that the Java compiler and JVM can use.

Annotations in Java are used to provide additional information, so it is an alternative option for XML and Java marker interfaces.

We’ll start with some built-in annotations before moving on to creating and using custom annotations.

Built-in Java Annotations

Java includes several built-in annotations. Some annotations are applied to Java code while others are applied to other annotations.

Built-In Java Annotations used in Java code

  • @Override
  • @SuppressWarnings
  • @Deprecated

Built-In Java Annotations used in other annotations

  • @Target
  • @Retention
  • @Inherited
  • @Documented

Understanding Built-In Annotations

Let’s start with the built-in annotations.

@Override

The annotation @Override ensures that the subclass method overrides the parent class method. If this is not the case, a compile time error occurs.

Sometimes we make silly mistakes, such as spelling mistakes. As a result, it is preferable to use the @Override annotation to ensure that the method is overridden.

class Animal{
void eatSomething(){System.out.println("eating something");}
}
class Dog extends Animal{
@Override
void eatsomething(){System.out.println("eating foods");}//should be eatSomething
}
class TestAnnotation1{
public static void main(String args[]){
Animal a = new Dog();
a.eatSomething();
}}

Output:

Compile Time Error

@SuppressWarnings

The @SuppressWarnings annotation is used to suppress compiler warnings.

import java.util.*;
class TestAnnotation2{
@SuppressWarnings("unchecked")
public static void main(String args[]){
ArrayList list = new ArrayList();
list.add("sonoo");
list.add("vimal");
list.add("ratan");
for(Object obj:list)
System.out.println(obj);
}}

Output :

Now no warning at compile time.

Because we are using a non-generic collection, removing the @SuppressWarnings(“unchecked”) annotation will result in a warning at compile time.

@Deprecated

The @Deprecated annotation indicates that this method is deprecated, and the compiler prints a warning. It notifies the user that it may be removed in future versions. As a result, it is preferable not to employ such methods.

class A{
void m(){System.out.println("hello m");}
@Deprecated
void n(){System.out.println("hello n");}
}
class TestAnnotation3{
public static void main(String args[]){
A a = new A();
a.n();
}}

At Compile time :

Note: Test.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

At Runtime :

hello n

Java Custom Annotations

Java Custom annotations, also known as Java User-defined annotations, are simple to create and use. An annotation is declared using the @interface element. As an example:

@interface MyAnnotations{}

MyAnnotation is the name of the custom annotation in this case. Considerations for Java Custom Annotation Signature

There are a few points that the programmer should keep in mind.

  1. There should be no throws clauses in the method.

2. The method should return one of the following data types: primitive, String, Class, enum, or an array of these data types.

3. There should be no parameters in the method.

4. To define annotation, we should add @ just before the interface keyword.

5. It may give the method a default value.

--

--

A passionate programmer, I am eager to challenge myself to do things I’ve never accomplished before and I strive to learn and improve on my skills every day