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;
1:public class SuppressWarningsTest {
2:
3: public static void main(String[] args) {
4: List names = new ArrayList<String>();
5: names.add("tim");
6:
7: }
8:
9:}
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 5. 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.
3 responses so far ↓
Java Custom Annotations « My experiments with technology - My oasis in the desert of Project Management // January 13, 2008 at 5:32 am |
[...] 13, 2008 at 5:32 am (Java annotations, custom annotation) My earlier post covered in-built annotations supported by Java 5. Today we will look at developing custom java [...]
vikram // April 29, 2009 at 10:47 am |
i think the way you tell about the annotation is not appropriate…so give more emphasis on the theortical part..
Mr. President // April 29, 2009 at 11:11 am |
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.