In this post, we will see what are annotations and why we use them.

What are annotations?

Annotations are used for META-DATA in Java. They are used to provide information to the runtime environment, compiler and software tools.
An annotation is created with following syntax:

@interface Interface_Name{
    
}

Then we can use our annotation like this:

@Interface_Name

Let us check out an example.

We will be creating classes for Husband and Wife.
We will also create an interface Person to put common methods in both Husband and Wife.
Then we will create an annotation ManagedBy that represents who wife or husband controls factory, house etc.

  1. Let us create a new Java project in Eclipse.
  2. Create a package named com.annot (or you can give any name)
  3. Next we create Person, Husband and Wife in the above package with following code:

    Person.java

    package com.annot;
    public interface Person {
       void nature();         // this is annotation variable but declared as a method
    }

    Husband.java

    package com.annot;
    public class Husband implements Person{
       @Override
       public void nature() {
          System.out.println("Loyal and hard working");
       }
    }

    Wife.java

    package com.annot;
    public class Wife implements Person{
       @Override
       public void nature() {
          System.out.println("Calm and helping");
       }
    }
  4. Now we will create our annotation.
    Manager.java

    package com.annot;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Manager {
       String name();
    }
  5. Next, we will create classes for house and factory. We will use our annotation to denote who controls them.

    House.java

    package com.annot;
    import java.lang.annotation.Annotation;
    @Manager(name="Husband")
    public class House {
       private Person person;
     
       public House(){
          Class newClass = House.class;
          for (Annotation annotation : newClass.getDeclaredAnnotations()) {
             Manager manager = (Manager) annotation;
     
             if(manager.name().toLowerCase().equals("wife"))
                 person = new Wife();
             else
                 person = new Husband(); 
          }
       }
       public void natureOfOwner() {
           person.nature();
       }
    }

    Factory.java

    package com.annot;
    import java.lang.annotation.Annotation;
    @Manager(name="Wife")
    public class Factory {
       private Person person;
     
       public Factory(){
          Class newClass = Factory.class;
          for (Annotation annotation : newClass.getDeclaredAnnotations()) {
             Manager manager = (Manager) annotation;
     
             if(manager.name().toLowerCase().equals("wife"))
                 person = new Wife();
             else
                 person = new Husband(); 
          }
       }
       public void natureOfOwner() {
           person.nature();
       }
    }
  6. Now let us test our code. We will create a new file Main.java with following code:
    package com.annot;
    public class Main {
       public static void main(String[] args) {
          Factory factory = new Factory();
          House house = new House();
     
          factory.natureOfOwner();
          house.natureOfOwner();
       }
    }

Now run the above class to check the result.