Annotations

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Annotations

Java predefined support in the form of packages,

the package contains 6-elements,

i. Classes

ii. Interfaces

iii. Exceptions

iv. Error

v. Enums

vi. Annotations

Annotations are not to develop the application just it is giving the metadata information to compiler.

General purpose annotation :

programming level annotations we will use at application level.

The Programming level annotations are present in "java.lang" package.

@Override

@SuppressWarnings

@Deprecated

@FuntionalInterface

@SafeVararg

Meta annotation:

Annotaion about annotations is called MetaAnnotation.

The meta annotations are giving information about general purpose annotations.

The meta annotations are present in "java.lang.annotation" package.

@Target

@Retention

@Documented

@Inherited
@Target : Indicates the contexts in which an annotation type is applicable.

CONSTRUCTOR

Constructor declaration

FIELD

Field declaration (includes enum constants)

LOCAL_VARIABLE

Local variable declaration

METHOD

Method declaration

PACKAGE

Package declaration

PARAMETER

Formal parameter declaration

TYPE

Class, interface (including annotation type), or enum declaration

TYPE_PARAMETER

Type parameter declaration

TYPE_USE

Use of a type

@Retention:

Indicates how long annotations with the annotated type are to be retained. If no Retention
annotation is present on an annotation type declaration, the retention policy defaults to
RetentionPolicy.CLASS

public static final RetentionPolicy SOURCE

Annotations are to be discarded by the compiler.

public static final RetentionPolicy CLASS

Annotations are to be recorded in the class file by the compiler but need not be
retained by the VM at run time. This is the default behavior.
public static final RetentionPolicy RUNTIME

Annotations are to be recorded in the class file by the compiler and retained by the
VM at run time, so they may be read reflectively.

@Documented:

Indicates that annotations with a type are to be documented by javadoc and similar tools by
default.

1.

@Override : To represnet the method is override method.

@Target(value=METHOD)

@Retention(value=SOURCE)

public @interface Override{

class MyThread extends Thread

{ @Override

public void run() {

2. @SuppressWarnings

To suppress the warning message.

@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})

@Retention(value=SOURCE)

public @interface SuppressWarnings{

}
public class Ex1 {

@SuppressWarnings("unchecked")

public static void main(String[] args) {

@SuppressWarnings("rawtypes")

ArrayList objs = new ArrayList();

objs.add(10);

objs.add(10.6);

objs.add("ratan");

3. @FunctionalInterface

To represent the interface is Functional.

@Documented

@Retention(value=RUNTIME)

@Target(value=TYPE)

public @interface FunctionalInterface{

@FunctionalInterface

interface Greeting

{ void greet();

4. @Deprecated

To represent the method is deprecated.

@Documented

@Retention(value=RUNTIME)

@Target(value={CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})

public @interface Deprecated{


}

ex:

package com.stahya;

class Python

{ @Deprecated

void course()

{ System.out.println("pythion classes are running");

public class Student {

public static void main(String[] args) {

Python python = new Python();

python.course();

A program element annotated @Deprecated is one that programmers are discouraged from using,
typically because it is dangerous, or because a better alternative exists.

5. @SafeVarArg

@Documented

@Retention(value=RUNTIME)

@Target(value={CONSTRUCTOR,METHOD})

public @interface SafeVarargs{

You might also like