Java Annotations

Home > Search
  by

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")

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments