Magazine
 
Java Developers Desk: Annotations

Suppose there is spell mistake in the method name such as the name is changed from toString to toStrimg. Then on compiling the code will generate the message like this:
Compiling 1 source file to D:tempNew Folder
(2)
TestJavaApplication1buildclasses
D:tempNew Folder
(2)TestJavaApplication1srctest
myannotationTest_Override.java:24: method
does not override a method from its
superclass
@Override
1 error
BUILD FAILED (total time: 0 seconds)

2) Deprecated annotation:
These types of annotations ensure that the compiler warns you when you use the deprecated element of the program. The example given below illustrates this concept. Example: Lets first create the class containing
the deprecated method.

public class Deprecated_method{
@Deprecated
public void showSomething() {
System.out.println(“Method has been
depricated’”);
}
}

Now lets try to invoke this method from inside the other class:

public class Test_Deprication {
public static void main(String arg[]) throws
Exception {
new Test_Deprication();
}
public Test_Deprication() {
Deprecated_method d = new
Deprecated_method();
d.showSomething();
}

The method showSomething() in the above example is declared as the deprecated method. That means we can’t further use this

 

method any more. On compiling the class Depricated_method does not generate any error. While compiling the class Test_Deprication generates the message like this:
Compiling 1 source file to D:tempNew Folder
(2)TestJavaApplication1buildclasses
D:tempNew Folder
(2)TestJavaApplication1srctestmyannotation
Test_Deprication.java:27:
warning: [deprecation] showSomething() in
test.myannotation.Deprecated_method has
been deprecated
d.showSomething();
1 warning

3) Suppresswarning annotation:

These types of annotations ensure that the compiler will shield the warning message in the annotated elements and also in all of its subelements. Lets take an example:

Suppose you annotate a class to suppress a warning and one of its method to suppress another warning, then both the warning will be suppressed at the method level only. Lets demonstrate it by an example:

public class Test_Depricated {
public static void main(String arg[]) throws
Exception {
new TestDepricated().showSomething();
}
@SuppressWarnings({“deprecation”})
public void showSomething() {
Deprecation_method d = new
Deprecation_method();
d.showSomething();
}
}

This example is suppressing the deprecation warnings that means we can’t see the warnings any more.

Note: Applying annotation at most deeply nested elements is a good idea. It is better to apply annotations at the method level rather than the class to annotate a particular method.

Dec 2007 | Java Jazz Up | 9
 
previous
index
next
 
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF