SlideShare a Scribd company logo
1 of 26
ANNOTATIONS
INTRODUCTION
 Java annotations are used to provide meta data for
your Java code. Being meta data, Java annotations
do not directly affect the execution of your code,
although some types of annotations can actually be
used for that purpose.
JAVA ANNOTATION PURPOSES
 Java annotations are typically used for the following
purposes:
 Compiler instructions
 Build-time instructions
 Runtime instructions
 Java has 3 built-in annotations that you can use to
give instructions to the Java compiler.
INSTRUCTIONS TO THE COMPILER
 There are three built-in annotations available in
Java
 @Deprecated,
 @Override &
 @SuppressWarnings
that can be used for giving certain instructions to the
compiler.
COMPILE-TIME INSTRUCTORS
 Annotations can provide compile-time instructions
to the compiler that can be further used by software
build tools for generating code, XML files etc.
COMPILE-TIME INSTRUCTORS
 Annotations can provide compile-time instructions
to the compiler that can be further used by sofware
build tools for generating code, XML files etc.
ANNOTATIONS BASICS
 An annotation always starts with the
symbol @ followed by the annotation name. The
symbol @ indicates to the compiler that this is an
annotation.
 For e.g. @Override
Here @ symbol represents that this is an
annotation and the Override is the name of this
annotation.
WHERE WE CAN USE ANNOTATIONS?

Annotations can be applied to the classes,
interfaces, methods and fields. For example the
below annotation is being applied to the method.
 @Override
 void myMethod()
 {
 //Do something
 }
BUILT-IN ANNOTATIONS IN JAVA
 Java has three built-in annotations:
 @Override
 @Deprecated
 @SuppressWarnings
@OVERRIDE:
 While overriding a method in the child class, we
should use this annotation to mark that method.
This makes code readable and avoid maintenance
issues, such as: while changing the method
signature of parent class, you must change the
signature in child classes (where this annotation is
being used) otherwise compiler would throw
compilation error. This is difficult to trace when you
haven’t used this annotation.
@DEPRECATED
 @Deprecated annotation indicates that the marked
element (class, method or field) is deprecated and
should no longer be used. The compiler generates
a warning whenever a program uses a method,
class, or field that has already been marked with
the @Deprecated annotation. When an element is
deprecated, it should also be documented using the
Javadoc @deprecated tag, as shown in the
following example. Make a note of case difference
with @Deprecated and @deprecated.
@deprecated is used for documentation purpose.
@SUPPRESSWARNINGS
 This annotation instructs compiler to ignore specific
warnings. For example in the below code, I am
calling a deprecated method (lets assume that the
method deprecatedMethod() is marked with
@Deprecated annotation) so the compiler should
generate a warning, however I am using
@@SuppressWarnings annotation that would
suppress that deprecation warning.
CREATING CUSTOM ANNOTATIONS
 Annotations are created by using @interface,
followed by annotation name as shown in the below
example.
 An annotation can have elements as well. They
look like methods. For example in the below code,
we have four elements. We should not provide
implementation for these elements.
 All annotations extends
java.lang.annotation.Annotation interface.
Annotations cannot include any extends clause.
NOTE:
 All the elements that have default values set while
creating annotations can be skipped while using
annotation. For example if I’m applying the above
annotation to a class then I would do it like this:
 @MyCustomAnnotation(
 studentName="Chaitanya",
 stuAddress="Agra, India" )
public class MyClass
{ ... }
NOTE:
 We can also have array elements in an annotation.
This is how we can use them:
Annotation definition:
 @interface MyCustomAnnotation
 { int count();
 String[] books(); }
 Usage:
@MyCustomAnnotation( count=3, books={"C++",
"Java"} )
public class MyClass { }
 we have used these four annotations:
 @Documented,
 @Target,
 @Inherited&
 @Retention.
 Lets discuss them in detail.
@DOCUMENTED
 @Documented annotation indicates that elements
using this annotation should be documented by
JavaDoc.
FOR EXAMPLE:
 java.lang.annotation.Documented
 @Documented
 public @interface MyCustomAnnotation {
 //Annotation body
 }
 @MyCustomAnnotation
 public class MyClass {
 //Class body
 }
 While generating the javadoc for class MyClass, the
annotation @MyCustomAnnotation would be
included in that.
@TARGET
 It specifies where we can use the annotation. For
example: In the below code, we have defined the
target type as METHOD which means the below
annotation can only be used on methods.
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Target;
 @Target({ElementType.METHOD})
 public @interface MyCustomAnnotation {
 }
 public class MyClass {
 @MyCustomAnnotation
 public void myMethod()
 {
 //Doing something
 }
 }
NOTE
 1) If you do not define any Target type that means
annotation can be applied to any element.
 2) Apart from ElementType.METHOD, an annotation can
have following possible Target values.
 ElementType.METHOD
 ElementType.PACKAGE
 ElementType.PARAMETER
 ElementType.TYPE
 ElementType.ANNOTATION_TYPE
 ElementType.CONSTRUCTOR
 ElementType.LOCAL_VARIABLE
 ElementType.FIELD
@INHERITED
 The @Inherited annotation signals that a custom
annotation used in a class should be inherited by all
of its sub classes. For example:
 java.lang.annotation.Inherited
 @Inherited
 public @interface MyCustomAnnotation {
 }
 @MyCustomAnnotation
 public class MyParentClass {
 ...
 }
 public class MyChildClass extends MyParentClass {
 ...
 }
@RETENTION
 It indicates how long annotations with the annotated
type are to be retained.
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 @Retention(RetentionPolicy.RUNTIME)
 @interface MyCustomAnnotation {

 }
 Here we have used RetentionPolicy.RUNTIME.
There are two other options as well. Lets see what
do they mean:
 RetentionPolicy.RUNTIME: The annotation should
be available at runtime, for inspection via java
reflection.
 RetentionPolicy.CLASS: The annotation would be
in the .class file but it would not be available at
runtime.
 RetentionPolicy.SOURCE: The annotation would be
available in the source code of the program, it
would neither be in the .class file nor be available at
the runtime.

More Related Content

What's hot

Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3Isham Rashik
 
Android Application Development - Level 2
Android Application Development - Level 2Android Application Development - Level 2
Android Application Development - Level 2Isham Rashik
 
Bt0074 oops with java
Bt0074 oops with javaBt0074 oops with java
Bt0074 oops with javaTechglyphs
 
ELENA MICROPROCESSOR
ELENA MICROPROCESSORELENA MICROPROCESSOR
ELENA MICROPROCESSORranjeetdon
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1Isham Rashik
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 
Application package
Application packageApplication package
Application packageJAYAARC
 
RomaFramework Tutorial Basics
RomaFramework Tutorial BasicsRomaFramework Tutorial Basics
RomaFramework Tutorial BasicsLuca Garulli
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to JavaSMIJava
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 

What's hot (20)

Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
Android Application Development - Level 2
Android Application Development - Level 2Android Application Development - Level 2
Android Application Development - Level 2
 
Bt0074 oops with java
Bt0074 oops with javaBt0074 oops with java
Bt0074 oops with java
 
ELENA MICROPROCESSOR
ELENA MICROPROCESSORELENA MICROPROCESSOR
ELENA MICROPROCESSOR
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
PHP 5
PHP 5PHP 5
PHP 5
 
Java features
Java featuresJava features
Java features
 
Application package
Application packageApplication package
Application package
 
RomaFramework Tutorial Basics
RomaFramework Tutorial BasicsRomaFramework Tutorial Basics
RomaFramework Tutorial Basics
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
 
first-applet
first-appletfirst-applet
first-applet
 
Java Programming Fundamentals
Java Programming Fundamentals Java Programming Fundamentals
Java Programming Fundamentals
 
C# XML documentation
C# XML documentationC# XML documentation
C# XML documentation
 
Ppt chapter07
Ppt chapter07Ppt chapter07
Ppt chapter07
 
java token
java tokenjava token
java token
 
Java
JavaJava
Java
 
The Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.NetThe Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.Net
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to Java
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 

Viewers also liked

Home enzymatic therapy administration program for lysosomal diseases in Spain...
Home enzymatic therapy administration program for lysosomal diseases in Spain...Home enzymatic therapy administration program for lysosomal diseases in Spain...
Home enzymatic therapy administration program for lysosomal diseases in Spain...Vicente Giner Galvañ
 
LOGS11_2015_OskariRankamo
LOGS11_2015_OskariRankamoLOGS11_2015_OskariRankamo
LOGS11_2015_OskariRankamoOskari Rankamo
 
Talking with Your Family about Hereditary Cancer
Talking with Your Family about Hereditary CancerTalking with Your Family about Hereditary Cancer
Talking with Your Family about Hereditary Cancerbkling
 
Paa bibliotecas escolares 2016 2017
Paa bibliotecas escolares 2016 2017Paa bibliotecas escolares 2016 2017
Paa bibliotecas escolares 2016 2017Risoleta Montez
 
Sarau sons & letras semana da leitura 2017
Sarau sons & letras semana da leitura 2017Sarau sons & letras semana da leitura 2017
Sarau sons & letras semana da leitura 2017joaocarlosoliveiracamurca
 
Quiz.Resultados.2017.8ºano
Quiz.Resultados.2017.8ºanoQuiz.Resultados.2017.8ºano
Quiz.Resultados.2017.8ºanoINAbiblioteca
 
Laporan Praktikum Kamaboko
Laporan Praktikum KamabokoLaporan Praktikum Kamaboko
Laporan Praktikum KamabokoErnalia Rosita
 
Pung resume 2017
Pung resume 2017Pung resume 2017
Pung resume 2017Elaine Pung
 
Gestão Pública: planejamento 2017
Gestão Pública: planejamento 2017Gestão Pública: planejamento 2017
Gestão Pública: planejamento 2017Aristides Faria
 
Fundamentos do Turismo (I): planejamento 2017
Fundamentos do Turismo (I): planejamento 2017Fundamentos do Turismo (I): planejamento 2017
Fundamentos do Turismo (I): planejamento 2017Aristides Faria
 

Viewers also liked (20)

Sarau sons e letras
Sarau sons e letrasSarau sons e letras
Sarau sons e letras
 
Home enzymatic therapy administration program for lysosomal diseases in Spain...
Home enzymatic therapy administration program for lysosomal diseases in Spain...Home enzymatic therapy administration program for lysosomal diseases in Spain...
Home enzymatic therapy administration program for lysosomal diseases in Spain...
 
LOGS11_2015_OskariRankamo
LOGS11_2015_OskariRankamoLOGS11_2015_OskariRankamo
LOGS11_2015_OskariRankamo
 
Talking with Your Family about Hereditary Cancer
Talking with Your Family about Hereditary CancerTalking with Your Family about Hereditary Cancer
Talking with Your Family about Hereditary Cancer
 
Rinascita 6k testimonial - Chinese
Rinascita 6k testimonial - ChineseRinascita 6k testimonial - Chinese
Rinascita 6k testimonial - Chinese
 
Peter drucker
Peter druckerPeter drucker
Peter drucker
 
Paa bibliotecas escolares 2016 2017
Paa bibliotecas escolares 2016 2017Paa bibliotecas escolares 2016 2017
Paa bibliotecas escolares 2016 2017
 
Sarau sons & letras semana da leitura 2017
Sarau sons & letras semana da leitura 2017Sarau sons & letras semana da leitura 2017
Sarau sons & letras semana da leitura 2017
 
KarenResumeDBA
KarenResumeDBAKarenResumeDBA
KarenResumeDBA
 
Quiz.Resultados.2017.8ºano
Quiz.Resultados.2017.8ºanoQuiz.Resultados.2017.8ºano
Quiz.Resultados.2017.8ºano
 
Laporan Praktikum Kamaboko
Laporan Praktikum KamabokoLaporan Praktikum Kamaboko
Laporan Praktikum Kamaboko
 
Question 4
Question 4Question 4
Question 4
 
Pung resume 2017
Pung resume 2017Pung resume 2017
Pung resume 2017
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Gestão Pública: planejamento 2017
Gestão Pública: planejamento 2017Gestão Pública: planejamento 2017
Gestão Pública: planejamento 2017
 
Genetica
GeneticaGenetica
Genetica
 
Tutorial QUIZIZZ
Tutorial QUIZIZZTutorial QUIZIZZ
Tutorial QUIZIZZ
 
Fundamentos do Turismo (I): planejamento 2017
Fundamentos do Turismo (I): planejamento 2017Fundamentos do Turismo (I): planejamento 2017
Fundamentos do Turismo (I): planejamento 2017
 
Ghisell brito
Ghisell britoGhisell brito
Ghisell brito
 
HIV, drugs and risk behaviour in Europe – what have we learnt and what are th...
HIV, drugs and risk behaviour in Europe – what have we learnt and what are th...HIV, drugs and risk behaviour in Europe – what have we learnt and what are th...
HIV, drugs and risk behaviour in Europe – what have we learnt and what are th...
 

Similar to Annotations

Similar to Annotations (20)

Annotations in Java with Example.pdf
Annotations in Java with Example.pdfAnnotations in Java with Example.pdf
Annotations in Java with Example.pdf
 
Java annotations
Java annotationsJava annotations
Java annotations
 
Understanding Annotations in Java
Understanding Annotations in JavaUnderstanding Annotations in Java
Understanding Annotations in Java
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Annotations
AnnotationsAnnotations
Annotations
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
Java Docs
Java DocsJava Docs
Java Docs
 
Java Annotation
Java AnnotationJava Annotation
Java Annotation
 
Java program structure
Java program structureJava program structure
Java program structure
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Java annotations
Java annotationsJava annotations
Java annotations
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
76829060 java-coding-conventions
76829060 java-coding-conventions76829060 java-coding-conventions
76829060 java-coding-conventions
 
Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8
 
Java notes
Java notesJava notes
Java notes
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
01slide
01slide01slide
01slide
 
01slide
01slide01slide
01slide
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 

Recently uploaded

SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 

Recently uploaded (20)

SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 

Annotations

  • 2. INTRODUCTION  Java annotations are used to provide meta data for your Java code. Being meta data, Java annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose.
  • 3. JAVA ANNOTATION PURPOSES  Java annotations are typically used for the following purposes:  Compiler instructions  Build-time instructions  Runtime instructions  Java has 3 built-in annotations that you can use to give instructions to the Java compiler.
  • 4. INSTRUCTIONS TO THE COMPILER  There are three built-in annotations available in Java  @Deprecated,  @Override &  @SuppressWarnings that can be used for giving certain instructions to the compiler.
  • 5. COMPILE-TIME INSTRUCTORS  Annotations can provide compile-time instructions to the compiler that can be further used by software build tools for generating code, XML files etc.
  • 6. COMPILE-TIME INSTRUCTORS  Annotations can provide compile-time instructions to the compiler that can be further used by sofware build tools for generating code, XML files etc.
  • 7. ANNOTATIONS BASICS  An annotation always starts with the symbol @ followed by the annotation name. The symbol @ indicates to the compiler that this is an annotation.  For e.g. @Override Here @ symbol represents that this is an annotation and the Override is the name of this annotation.
  • 8. WHERE WE CAN USE ANNOTATIONS?  Annotations can be applied to the classes, interfaces, methods and fields. For example the below annotation is being applied to the method.  @Override  void myMethod()  {  //Do something  }
  • 9. BUILT-IN ANNOTATIONS IN JAVA  Java has three built-in annotations:  @Override  @Deprecated  @SuppressWarnings
  • 10. @OVERRIDE:  While overriding a method in the child class, we should use this annotation to mark that method. This makes code readable and avoid maintenance issues, such as: while changing the method signature of parent class, you must change the signature in child classes (where this annotation is being used) otherwise compiler would throw compilation error. This is difficult to trace when you haven’t used this annotation.
  • 11. @DEPRECATED  @Deprecated annotation indicates that the marked element (class, method or field) is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field that has already been marked with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. Make a note of case difference with @Deprecated and @deprecated. @deprecated is used for documentation purpose.
  • 12. @SUPPRESSWARNINGS  This annotation instructs compiler to ignore specific warnings. For example in the below code, I am calling a deprecated method (lets assume that the method deprecatedMethod() is marked with @Deprecated annotation) so the compiler should generate a warning, however I am using @@SuppressWarnings annotation that would suppress that deprecation warning.
  • 13. CREATING CUSTOM ANNOTATIONS  Annotations are created by using @interface, followed by annotation name as shown in the below example.  An annotation can have elements as well. They look like methods. For example in the below code, we have four elements. We should not provide implementation for these elements.  All annotations extends java.lang.annotation.Annotation interface. Annotations cannot include any extends clause.
  • 14. NOTE:  All the elements that have default values set while creating annotations can be skipped while using annotation. For example if I’m applying the above annotation to a class then I would do it like this:  @MyCustomAnnotation(  studentName="Chaitanya",  stuAddress="Agra, India" ) public class MyClass { ... }
  • 15. NOTE:  We can also have array elements in an annotation. This is how we can use them: Annotation definition:  @interface MyCustomAnnotation  { int count();  String[] books(); }  Usage: @MyCustomAnnotation( count=3, books={"C++", "Java"} ) public class MyClass { }
  • 16.  we have used these four annotations:  @Documented,  @Target,  @Inherited&  @Retention.  Lets discuss them in detail.
  • 17. @DOCUMENTED  @Documented annotation indicates that elements using this annotation should be documented by JavaDoc.
  • 18. FOR EXAMPLE:  java.lang.annotation.Documented  @Documented  public @interface MyCustomAnnotation {  //Annotation body  }  @MyCustomAnnotation  public class MyClass {  //Class body  }  While generating the javadoc for class MyClass, the annotation @MyCustomAnnotation would be included in that.
  • 19. @TARGET  It specifies where we can use the annotation. For example: In the below code, we have defined the target type as METHOD which means the below annotation can only be used on methods.
  • 20.  import java.lang.annotation.ElementType;  import java.lang.annotation.Target;  @Target({ElementType.METHOD})  public @interface MyCustomAnnotation {  }  public class MyClass {  @MyCustomAnnotation  public void myMethod()  {  //Doing something  }  }
  • 21. NOTE  1) If you do not define any Target type that means annotation can be applied to any element.  2) Apart from ElementType.METHOD, an annotation can have following possible Target values.  ElementType.METHOD  ElementType.PACKAGE  ElementType.PARAMETER  ElementType.TYPE  ElementType.ANNOTATION_TYPE  ElementType.CONSTRUCTOR  ElementType.LOCAL_VARIABLE  ElementType.FIELD
  • 22. @INHERITED  The @Inherited annotation signals that a custom annotation used in a class should be inherited by all of its sub classes. For example:
  • 23.  java.lang.annotation.Inherited  @Inherited  public @interface MyCustomAnnotation {  }  @MyCustomAnnotation  public class MyParentClass {  ...  }  public class MyChildClass extends MyParentClass {  ...  }
  • 24. @RETENTION  It indicates how long annotations with the annotated type are to be retained.
  • 25.  import java.lang.annotation.Retention;  import java.lang.annotation.RetentionPolicy;  @Retention(RetentionPolicy.RUNTIME)  @interface MyCustomAnnotation {   }
  • 26.  Here we have used RetentionPolicy.RUNTIME. There are two other options as well. Lets see what do they mean:  RetentionPolicy.RUNTIME: The annotation should be available at runtime, for inspection via java reflection.  RetentionPolicy.CLASS: The annotation would be in the .class file but it would not be available at runtime.  RetentionPolicy.SOURCE: The annotation would be available in the source code of the program, it would neither be in the .class file nor be available at the runtime.