- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Built In Annotations
Java defines many built-in annotations. Most of these are specialized, but nine are general purpose. Of these, four are imported from java.lang.annotation: @Retention, @Documented, @Target, and Inherited. Five, @Override, @Deprecated, @FunctionInterface, @SafeVarargs, and @SuppressWarnings, are included in java.lang.
Let's discuss these built-in annotations one by one.
@Retention
The @Retention is designed to be used only as an annotation to another annotation. This specifies the retention policy.
@Documented
The @Documented annotation is a marker interface that tells a tool, an annotation is to be documented. It is designed to be used only as an annotation to an annotation declaration.
@Target
The @Target annotation specifies the types of items to which an annotation can be applied. It is designed to be used only as an annotation to another annotation.
@Target takes only one argument, which is an array of constants of the ElementType enumeration. This argument determines the types of declarations to which the annotation can be applied. The constants are shown here (in this table) along with the type of declaration to which they corresponds :
Target Constant | Annotation Can Be Applied To |
---|---|
ANNOTATION_TYPE | Another annotation |
CONSTRUCTOR | Constructor |
FIELD | Field |
LOCAL_VARIABLE | Local variable |
METHOD | Method |
PACKAGE | Package |
PARAMETER | Parameter |
TYPE | Class, interface, or enumeration |
TYPE_PARAMETER | Type parameter (added by JDK 8) |
TYPE_USE | Type use (added by JDK 8) |
You can specify one or more of these values in a @Target annotation. To specify multiple values, you must specify them within a braces-delimited list. For instance, to specify that an annotation applies only to the fields and the local variables, you can use this @Target annotation:
@Target( { ElementType.FIELD, ElementType.LOCAL_VARIABLE } )
If you don't use @Target, then except for type parameters, the annotation can be used on any declaration. For this cause, it is often a good idea to explicitly specify the target/targets so as to clearly indicate the intended uses of an annotation.
@Inherited
The @Inherited is a marker annotation that can be used only on another annotation declaration. Furthermore, it affects only annotations which will be used on class declarations.
@Inherited causes the annotation for a superclass to be inherited by a subclass. So, when a request for a specific annotation is made to the subclass, if that annotation isn't present in the subclass, subsequently its superclass is checked. If that annotation is present in the superclass, and if it is annotated with @Inherited, then that annotation will be returned.
@Override
The @Override is a marker annotation that can be used only on methods.
A method annotated with @Override must override a method from a superclass. If it does not, a compile-time error will produce. It is used to ensure that a superclass method is actually overridden, and not simply overloaded.
@Deprecated
The @Deprecated is a marker annotation. It indicates that a declaration is absolete and has been replaced by a newer form.
@FunctionalInterface
The @FunctionInterface is a marker annotation added by the JDK 8 and designed for use on interfaces. It indicates that an annotated interface is a functional interface.
A functional interface is an interface that contains only one abstract method. Functional interfaces are used by the lambda expressions. If the annotation interface is not a functional interface, a compilation error will be produced.
It is important to understand that @FunctionInterface is not required to create a functional interface. Any interface with only one abstract method is, by definition, a functional interface. Thus, @FunctionInterface is purely informational.
@SafeVarargs
The @SafeVarargs is a marker annotation that can be applied to methods and constructors. It indicates that no unsafe actions linked to a varargs parameter occur. It is used to suppress unchecked warnings on otherwise safe code as it refers to non-reifiable varargs types and parameterized array instantiation. A non-reifiable type is, essentially a generic type. It must be applied only to varargs methods or constructors that are static or final.
@SuppressWarnings
The @SuppressWarnings specifies that one or more warnings that might be issued by the compiler are to be suppressed. The warnings to suppress are specified by name in string form.
« Previous Tutorial Next Tutorial »