Java Annotations – An Introduction

Annotations is a new feature introduced in Java 5 (Tiger release). This post intends to provide a high level introduction to the concept of annotations.

Java provides three in built annotations, namely Override, Deprecated and SuppressWarnings. Let’s look at the first annotation type Override. Consider the following code:
Class A:

package com.test.annotations;

public class A {

    public void doSomething() {

    }

}

Class B:

package com.test.annotations;

public class B extends A {

    public void doSomething(){

    }

}

Class B extends A. We want class B to override the doSomething method defined in class A. Due to oversight it is possible that the method name could be misspelt or the method signature changed causing the override behaviour to remain unimplemented. To ensure that such mistakes do not happen we have the Override annotation. Let’s change the Class B implementation to the following.

package com.test.annotations;

public class B extends A {

    @Override
    public void doSomething(){

    }

}

The addition of the annotation forces the compiler to verify if the doSomething method in the parent class is actually overridden. Let’s assume that the method name is changed from doSomething to dosomething. The Override annotation causes the compiler to throw the following error message “The method dosomething of B must override a superclass method” Moving on to the next standard annotation Deprecated. Consider the following class DeprecatedTest:

package com.test.annotations.deprecated;

public class DeprecatedTest {

    @Deprecated
    public void test() {

    }

    public static void main(String args[]) {
	DeprecatedTest dpTest = new DeprecatedTest();
	dpTest.test();
    }

}

The method test has been marked deprecated using the deprecated annotation. In Eclipse the usage of the deprecated method is marked using a strikethrough. Java compiler will show a warning message like “warning: test method of DeprecatedTest class is deprecated”. To force the compiler to show warnings run javac using the -deprecated or -Xlint:deprecated option.

Let’s look at the third standard annotation available in the Java 5 JDK i.e. SuppressWarnings. Consider the following sample class:

package com.test.annotations.suppresswarns;

import java.util.ArrayList;
import java.util.List;

public class SuppressWarningsTest {

    public static void main(String[] args) {
	List names = new ArrayList<String>();
	names.add("tim");

    }

}

The warning message “The method add(Object) belongs to the raw type List. References to generic type List<E> should be parameterized.” is generated at line 10. In case we do not want to view the warning messages, change the SuppressWarningsTest class’s code to the following:

package com.test.annotations.suppresswarns;

import java.util.ArrayList;
import java.util.List;

public class SuppressWarningsTest {

    @SuppressWarnings(value={"unchecked"})
    public static void main(String[] args) {
	List names = new ArrayList<String>();
	names.add("tim");
    }

}

The warning message is removed. Note my current observations are based on running the code in Eclipse environment. The compiler generated error/warning messages should be similar. In the next post I intend to look at developing custom annotations and their possible scenarios for use.

Advertisements

4 thoughts on “Java Annotations – An Introduction

    1. The aim of my blog is to look at different technologies and provide a short tutorial on how one can implement or practically use them in our daily software development ritual. The emphasis is more on examples to illustrate usage as compared to providing theoretical inputs. You will easily find a number of articles/blogs on explaining the theory.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s