Prev Next

Java / Annotations

Could not find what you were looking for? send us the question and we would be happy to answer your question.

Explain Annotations in Java.

Java Annotations are metadata that provides data about the program which is not part of the program itself. The annotation has no direct impact on the operation of the code it annotates. Java Annotations are introduced in Java 5.

The below are some of the uses of Annotations.

Annotation provide information to the compilers to detect errors and suppress warnings.

Facilitates compile time and deployment time processing so that other tools can generate code, xml files and so on.

Some annotations allow runtime processing such as interception.

List few annotations defined in Java SDK.

Many annotations are defined in java.lang and java.lang.annotation package.

@Deprecated annotation indicates that the element is deprecated and no longer in use. The compiler issues a warning when such element- Class, method or field with @Deprecated annotation is used.

@Override annotation specifies that the element overrides its super class definition.

@SuppressWarnings suppresses the compiler warnings that it would generated.

@SafeVarargs suppresses unchecked warning related to varArgs usage and asserts that the code performs only safe operations on the varArg parameters.

@FunctionalInterface identifies a functional interface as part of Java 8 specification.

Define meta-annotations in Java.

Meta-annotations are annotations that is applied to other annotation.

@Target meta-annotation is an example that defines what element types be marked by the annotation.

List few meta-annotations defined in Java SDK.

@Retention meta-annotation indicates how long the annotation to be retained or visible.

@Documented indicates that the annotated element be documented using the Javadoc tool since annotations are not included by Javadoc tool by default.

@Inherited specifies that an annotation types is automatically inherited. If an inherited meta-annotation exists on an annotation type declaration, when the user queries the annotation type on a class declaration that has no annotation for this type, then class's super class be automatically queried for this annotation type.

@Repeatable indicates that the marked annotation can be applied more than once on the same declaration.

Where do I apply annotations in a Java program?

Java Annotations can be applied to Java element declarations and as of Java 8, annotations are applicable to the types.

The Annotations on element declarations include classes, fields, methods, local variables, and other elements.

Annotations on types include object creation, type casting, implements clause and throws exception declaration. This form of annotation is known as type annotation.

How do I create a custom Java Annotation?

Java Annotation definition resembles an interface definition where the keyword interface is preceded by @. @ represents Annotation type (AT).

The Java Annotation body contains annotation type element declarations that look like method and also can define an optional default value.

package net.javapedia.annotations;

public @interface CustomAnnotation {

	int value();

}

package net.javapedia.annotations;

@CustomAnnotation
public class AnnotatedClass {

}

What does @SuppressWarnings static-access perform in Java?

static-access suppresses the compiler warnings related to incorrect static access.

Explain repeating annotation in Java8.

Prior to Java 8, attaching more than one annotation of the same type to the same part of the code (for example, a class or a method) was not allowed. Therefore, the developers had to group them together into single container annotation as a workaround:

@Authors({
    @Author(name = "John"),
    @Author(name = "George")
})
public class Book { ... }

Java 8 introduces repeating annotations which allows to rewrite the same annotation without explicitly using the container annotation:

@Author(name = "John")
@Author(name = "George")
public class Book { ... }

The container annotation is still used but this time the Java compiler is responsible for wrapping the repeating annotations into a container annotation.

User-defined annotations are not repeatable by default and have to be annotated with @Repeatable annotation.

What are the 2 attributes added in @Deprecated annotation in java 9?

forRemoval and since are the 2 new attributes.

@Deprecated (forRemoval=true, since="012019")

forRemoval attribute indicate that the annotated method will not be used in future release if value is true. Default value is false. since attribute take a String value that informs from which release the annotated method is Deprecated.

«
»
XML

Comments & Discussions