Annotations
Annotations
---------------------
Annotations concept has came in to market from sun micro systemt for JDk1.5 version
Actually XMl file data .java container is not understanding . To understand the xml
file data to java container .
parser s/w is used to read the xml , validate the xml , parse the xml
to read, validate , parse the xml . parser s.w will take some times
when parser s.w will take some time. Application performance is very low.
To over come xml file approch Annotains has came in to market form jdk 1.5 v on
words.
@Override:
--------
This annotains is used to override the super class method in to sub class .
Note :
----
As a programer's we know super class method overrding in to sub class . But
java compiler doesn't know super class method has overdine in to sub class or not
Dervied.java
-------------
package com.nisum.it;
@Override
public void m()
{
System.out.println("m1() of Dervied");
}
}
main
---
package com.nisum.it;
ver. m();
Error:
at com.nisum.it.Test.main(Test.java:9)
@SupressWarnings:
-----------------
package com.nisum.it;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public static void main(String args[])
{
list.add("raja");
list.add("venu");
list.add("prabhu");
System.out.println(list);
for (Object obj:list)
{
System.out.println(obj);
}
}
}
Note:
-----
@Deprecated :
-------------
This annotaion is used to mark the method as a deprecate.
when @Deprecated annotaion mark the method as a deprecate. Then Java compiler will
print the warning.
Example:
----------
class ODemo1
{
@Deprecated
public void m2()
{
System.out.println("This is a not a main method");
}
}
de.m2();
}
}
Error:
-----
D:\NARESH>javac Test.java
Note: Test.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
CustomAnnotion:
-----------------
Types of annotains:
----------------
1) Marker Annotaion
2) Single value Annotaion
3) Multi value Annotaion
Marker Annotaion:
----------------
if any Annotaion is not having any method then that annotaion is called
Marker Annotaion
Ex:
--
@interface MyAnnotaion{
}
Ex:
---
@interface MyAnnotaion
{
int value();
}
To apply the default value for custom annotaion .We should use default key word.
Ex:
---
@interface MyAnnotaion
{
int value() default 0;
can
@ MyAnnotaion(value=10)
Ex:
@interface MyAnnotaion
{
int value1();
String value2();
String value3();
}
to apply the value for multi value Annotion . we have to follow below mention
order
@interface MyAnnotaion
{
int value1() default 1;
String value2 () default "";
String value23() default "xyz";
}
@Target:
-------
This annotaion is used to specify the type of Element.
Example:
-----
how to specify the annotaion for class:
------------------------------------------
@Target(ElementType.TYPE)
@interface MyAnnotaion
{
int value1();
String value2();
}
@Retention :
-----------
Example :
---------
@Retention(Retentionpolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotaion
{
int value1();
String value2();
}
// Applying annotation
Class Hello
{
@MyAnnotation(value=10)
}
}
// Acessing Annotaion
Class Test
{
public static void main(String args[]) throws Exception
{
Method d = hello.getClass().getMethod("sayHello");
System.out.println(d);
MyAnnotation mmm = d.getAnnotation(MyAnnotation.class);
System.out.println("value is:"mmm.value());
}
}
output:
=-----
value is : 10