[go: up one dir, main page]

0% found this document useful (0 votes)
27 views8 pages

Annotations

The document discusses Java annotations including built-in annotations like @Override and @Deprecated, how to create custom annotations, and how annotations can be used to provide metadata about programs. Annotations allow alternative ways to return objects and were introduced to replace the XML approach.

Uploaded by

Harshal More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

Annotations

The document discusses Java annotations including built-in annotations like @Override and @Deprecated, how to create custom annotations, and how annotations can be used to provide metadata about programs. Annotations allow alternative ways to return objects and were introduced to replace the XML approach.

Uploaded by

Harshal More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

 Java Annotations :

---------------------

Annotations is used to provide the some meta data about program.

Meta data means data about data

Annotations concept has came in to market from sun micro systemt for JDk1.5 version

every Annotation is a interface in java

every annotation is denotating with @ symbal

Annotaions is a alternative way to return the object as pure


programinglanaguage.

Annotaions has came in to market to replace xml approch.

Actually XMl file data .java container is not understanding . To understand the xml
file data to java container .

java container's needs to use parser s/w.

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.

what are the built in annotaions in market for JSE:


----------------------------------------------------
@Override
@supressWarnings
@Deprecated
@Target
@Documented
@inherited
@Retention

@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

to infrom java compiler we should use @Override Annotaions.

Example of @Override Annotaion:


-------------------------------
Base.java
---------
package com.nisum.it;

public class Base {

public void m1()


{
System.out.println("m1() of Base");
}
}

Dervied.java
-------------
package com.nisum.it;

public class Dervied extends Base{

@Override
public void m()
{
System.out.println("m1() of Dervied");
}
}

main
---
package com.nisum.it;

public class Test {

public static void main(String[] args) {

Base ver = new Dervied();

ver. m();

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:


The method m() is undefined for the type Base

at com.nisum.it.Test.main(Test.java:9)

@SupressWarnings:
-----------------

This annotaion is used to supresswarning issue by compiler.

package com.nisum.it;

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

public class Test1 {

@SuppressWarnings("unchecked")
public static void main(String args[])
{

List list= new ArrayList();

list.add("raja");
list.add("venu");
list.add("prabhu");

System.out.println(list);
for (Object obj:list)
{
System.out.println(obj);
}

}
}

Error: No warning at compile time error

Note:
-----

Let's suppose if we remove @suppressWarnings("unchecked) annotaion. It will show


warining at compile time

why because we are using non generic collection.

@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.

So Javacompiler will infrom to user . IT may be remove in further version.

Example:
----------
class ODemo1
{

public void m1()


{
System.out.println("this is a main method--");
}

@Deprecated
public void m2()
{
System.out.println("This is a not a main method");
}
}

public class Test


{
public static void main(String[] args)
{

ODemo1 de= new ODemo1();

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:
-----------------

If we want to create Custom Annotation . We must use @interface element


if we use @interface element we can create custom annotion
Ex:
----
@interface MyAnnotaion{
}

To create the custom Annotation we have to follow below metnion rules:


-------------------------------------------------------------------------

1) we should not define any parmeter for method.


2) we should return any one following data type for method
like primtivate datatype, String , Classes, enum or array .

3) we should not define any throws clauses for method.

4) we should add @ symbal before interface keyword to define custom annotaion.

5) we should assign defalult value for method .

Types of annotains:
----------------

we have three 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{
}

Single value Annotaion:


----------------------
If any Annotaion is having at least one method then that Annotaion is called Single
value Annotaions

Ex:
---

@interface MyAnnotaion
{

int value();
}

can we apply default value for single value Annotaion:


----------------------------------------------------

yes we can apply default value for single value Annotaion.

To apply the default value for custom annotaion .We should use default key word.

Ex:
---
@interface MyAnnotaion
{
int value() default 0;

How to apply value for Single value Annotaion:


---------------------------------------------
to apply the value for single value Annotaion we have to follow below mention
order.

can

@ MyAnnotaion(value=10)

Multi value Annotaion:


--------------------
if any annotaion is having at most more than one method then that annotaion is
called
Multi value Annotaion

Ex:

@interface MyAnnotaion
{
int value1();
String value2();
String value3();
}

how to apply the value for multi value Annotion :


--------------------------------------------

to apply the value for multi value Annotion . we have to follow below mention
order

@My Annotaion(value1="10", value2="Rajesh", @value3="karthik")

Can we apply the default value for multi value Annotaion :


-----------------------------------------------------

yes we can apply default value fro multi value Annotaion.

@interface MyAnnotaion
{
int value1() default 1;
String value2 () default "";
String value23() default "xyz";
}

@Target:
-------
This annotaion is used to specify the type of Element.

we can apply this annotaion below mention elementstype

Element Types where can we applied


------------ -------------------
1) TYPE ---> class, enum, interface

2) FIELD ----> fileds

3) MeTHOD ----> methods

4) CONSTRUCTOR --> constructor.

5) LOCAL _VARIABLES --> local varibles

6) ANNOTATION _TYPE --> annotation type

7) PARAMETER --> parameter.

Example:
-----
how to specify the annotaion for class:
------------------------------------------
@Target(ElementType.TYPE)
@interface MyAnnotaion
{
int value1();
String value2();
}

how to specify the annotaion for class, method, fileds:


------------------------------------------------------

@Target(ElementType.TYPE, ElementType.FIELD, ElementTYpe.METHOD)


@interface MyAnnotaion
{
int value1();
String value2();
}

@Retention :
-----------

It is used to sepecify the what leveal of annotaion will be availble

Retention policy Avalibity:


--------------- __________

Retentionpolicy.Source -----> It is refering to source code , but it will not


availble in complied class

Retentionpolicy.Class ----> It is refering to .class file, availble in java


compiler but not availble in JVM
It inculded class files.

Retentionpolicy.RUNTIME ---> It refers to runtime , availble in java compiler and


JVM.

Example :

---------
@Retention(Retentionpolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotaion

{
int value1();
String value2();
}

how to create custom annotaion and how to apply custom annotaion :


-----------------------------------------------------------------

// creating custom annotation


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ interface MyAnnotation
{
int value();

// Applying annotation

Class Hello
{
@MyAnnotation(value=10)

public void sayHello()


{

System.out.println(" say hello() is used to apply annotation");

}
}

// Acessing Annotaion

Class Test
{
public static void main(String args[]) throws Exception
{

Hello hello = new Hello();

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

You might also like