Here the compiler would expect the name of Test() of Derivedclass to match that of the test() in Baseclass.Code will compile if its changed to test().By this annotation , compiler ensures that we have over ridden the method correctly avoiding any typing errors.
2.@deprecated:
Some times there might be a function written with more powerful feature than the existing one. As most of the people might have used the older one, to give them time to accommodate new change as well as to avoid using it again and warn the developer that the function may be removed anytime in future, methods are deprecated.
@deprecated is a built in annotation used to let the developer know, they are using a deprecated method.
Example:
public class Annotatefunction {
public static void main(String[] args) {
Derivedclass dc = new Derivedclass();
String result = dc.test();
// test is shown as deprecated though no error is thrown
}
}
class Derivedclass extends Annotatefunction
{
@Deprecated
public String test()
{
return "";
}
}
3. @SuppressWarnings :
Here is a simple example to show the usage of SupressedWarning..
As the name says, it is used to suppress any of the warning messages thrown by the compiler.
For example.,
public class Annotatefunction {
public static void main(String[] args) {
Derivedclass dc = new Derivedclass(); //IDE shows warning that value of the local variable is not used.
}
}
class Derivedclass extends Annotatefunction
{
@Deprecated
public String test()
{
System.out.println("hi");
return "true";
}
}
Introducing this annotation for the main method., the warning could be suppressed.
@SuppressWarnings("unused")
public static void main(String[] args) {
Derivedclass dc = new Derivedclass(); //no warning is thrown now
}
For the list of parameters used for suppresswarnings, please refer to the eclipse documentation.
at
ECLIPSE document site.
For quicker reference, i have pasted the content from above website...
allDeadCode | dead code including trivial if(DEBUG) check |
allDeprecation | deprecation even inside deprecated code |
allJavadoc | invalid or missing javadoc |
assertIdentifier | occurrence of assert used as identifier |
boxing | autoboxing conversion |
charConcat | when a char array is used in a string concatenation without being converted explicitly to a string |
compareIdentical | comparing identical expressions |
conditionAssign | possible accidental boolean assignment |
constructorName | method with constructor name |
deadCode | dead code excluding trivial if (DEBUG) check |
dep-ann | missing @Deprecated annotation |
deprecation | usage of deprecated type or member outside deprecated code |
discouraged | use of types matching a discouraged access rule |
emptyBlock | undocumented empty block |
enumIdentifier | occurrence of enum used as identifier |
enumSwitch | incomplete enum switch |
fallthrough | possible fall-through case |
fieldHiding | field hiding another variable |
finalBound | type parameter with final bound |
finally | finally block not completing normally |
forbidden | use of types matching a forbidden access rule |
hashCode | missing hashCode() method when overriding equals() |
hiding | macro for fieldHiding, localHiding, typeHiding and maskedCatchBlock |
indirectStatic | indirect reference to static member |
intfAnnotation | annotation type used as super interface |
intfNonInherited | interface non-inherited method compatibility |
intfRedundant | find redundant superinterfaces |
javadoc | invalid javadoc |
localHiding | local variable hiding another variable |
maskedCatchBlock | hidden catch block |
nls | non-nls string literals (lacking of tags //$NON-NLS-<n>) |
noEffectAssign | assignment with no effect |
null | potential missing or redundant null check |
nullDereference | missing null check |
over-ann | missing @Override annotation |
paramAssign | assignment to a parameter |
pkgDefaultMethod | attempt to override package-default method |
raw | usage a of raw type (instead of a parametrized type) |
semicolon | unnecessary semicolon or empty statement |
serial | missing serialVersionUID |
specialParamHiding | constructor or setter parameter hiding another field |
static-access | macro for indirectStatic and staticReceiver |
staticReceiver | if a non static receiver is used to get a static field or call a static method |
super | overriding a method without making a super invocation |
suppress | enable @SuppressWarnings |
syncOverride | missing synchronized in synchronized method override |
syntheticAccess | when performing synthetic access for innerclass |
tasks | enable support for tasks tags in source code |
typeHiding | type parameter hiding another type |
unchecked | unchecked type operation |
unnecessaryElse | unnecessary else clause |
unqualifiedField | unqualified reference to field |
unused | macro for unusedArgument, unusedImport, unusedLabel, unusedLocal, unusedPrivate and unusedThrown |
unusedArgument | unused method argument |
unusedImport | unused import reference |
unusedLabel | unused label |
unusedLocal | unused local variable |
unusedPrivate | unused private member declaration |
unusedThrown | unused declared thrown exception |
unusedTypeArgs | unused type arguments for method |
uselessTypeCheck | unnecessary cast/instanceof operation |
varargsCast | varargs argument need explicit cast |
warningToken | unhandled warning token in @SuppressWarnings |
4.@Retention:
We can build our own annotations like we declare an interface.
This is how it is done.
Example:
@interface MyFirstAnnotation
{
String method1();
String method2();
}
@Retention are meta annotations and this with its enumerated constants tells how long the annotation has to be retained.
It has three constants.
1. RetentionPolicy.CLASS
2.
RetentionPolicy.SOURCe
3.
RetentionPolicy.RUNTIME
Default value is RetentionPolicy.CLASS. i.e. If there is no retention policy specified for the annotation then by default it applies the CLASS one.RetentionPolicy.CLASS will store the annotation in class file, but is not available in runtime for any external tool or api(like java refelection) to access.
RetentionPolicy.Runtime will store the annotation in class file and is available in runtime for accessing.
RetentionPolicy.Source will not store the annotation in class file.
Example of how to use the retention annotation on the above code snippet.
@Retention(RetentionPolicy.SOURCE)
@interface MyFirstAnnotation
{
String method1();
String method2();
}
@ symbol before interface says that it is an annotation.