Java - Annotations

by
Jeremy Canfield |
Updated: March 23 2022
| Java articles
In Java, an annotation is any text preceded by the @ character. Following are a few fairly common annotations.
@Override
@Remote
@Stateless
As a practice example, this code contains two identical methods (myMethod). If the @Override annotion is not used, myMethod in the child class would behave as the main method. Using @Override instructs the compiler to use myMethod in the child class as a sub-method.
public class ParentClass {
public void myMethod() {
System.out.println("Parent method");
}
}
public class ChildClass extends ParentClass {
@Override
public void myMethod() {
System.out.println("Child method");
}
}
Sometimes, annotations are used instead of comments, since annotation can instruct the compiler to do something, where comments have not impact on the compiler.
// John Doe
// Jane Doe
An annotation can contain key value pairs.
@People(
id = "1",
name = "John Doe"
)
The same annotation can be used more than once.
@People(name = "John Doe")
@People(name = "Jane Doe")
Did you find this article helpful?
If so, consider buying me a coffee over at