0% found this document useful (0 votes)
49 views5 pages

Annotations

Annotations provide metadata information to the compiler. There are general purpose annotations like @Override and @SuppressWarnings that are used at the programming level. Meta annotations like @Target and @Retention provide information about other annotations. Common general purpose annotations include @Override to indicate an overridden method, @SuppressWarnings to suppress compiler warnings, @Deprecated to mark deprecated elements, @FunctionalInterface for functional interfaces, and @SafeVarargs to suppress varargs warnings.

Uploaded by

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

Annotations

Annotations provide metadata information to the compiler. There are general purpose annotations like @Override and @SuppressWarnings that are used at the programming level. Meta annotations like @Target and @Retention provide information about other annotations. Common general purpose annotations include @Override to indicate an overridden method, @SuppressWarnings to suppress compiler warnings, @Deprecated to mark deprecated elements, @FunctionalInterface for functional interfaces, and @SafeVarargs to suppress varargs warnings.

Uploaded by

Bhaskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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