Ch13 Annotations Flashcards
What is the purpose of annotations?
The purpose of an annotation is to assign metadata attributes to classes, methods, variables, and other Java types.
Annotations function a lot like interfaces. Why use annotations over interfaces?
While interfaces can be applied to classes only, annotations can be applied to any declaration including classes, methods, expressions, and even other annotations.
Annotations also allow us to pass a set of values where they are applied.
What is the simplest form to define an annotation?
public @interface AnnotationName { }
Is the following code valid?
public @interface Exercise {
int hoursPerDay();
}
@Exercise(hoursPerDay = 3) public class ZooEmployee { }
This code is valid.
Is the following code valid?
public @interface Exercise {
int hoursPerDay();
int startHour() default 6;
}
@Exercise(hoursPerDay = “3”) public class ZooEmployee { }
It won’t compile because the type given for hoursPerDay is not compatible with type int.
Given the following code, how do we make hoursPerDay() default to value 24?
public @interface Exercise {
int hoursPerDay();
}
int hoursPerDay() default 24;
Can the default value of an annotation be null?
No, it must be a non-null constant expression.
What are the supported types of an annotation element?
A primitive type, a String, a Class, an enum, another annotation, or an array of any of these types (not multi-dimensional arrays).
Annotation elements are implicitely …
public and abstract
Can marker annotations contain constant variables?
Yes, they are not considered elements.
When can annotations be applied without paranthesis?
When the annotation is a marker annotation (contain no elements).
Where can annotations be applied?
- Classes, interfaces, enums and modules
- Variables (static, instance, local)
- Methods and constructors
- Method, constructor, and lambda parameters
- Cast expressions
- Other annotations
Which annotation can be used to specify which decleration type they can be applied to?
The @Target annotation.
How do you mark an annotation element as required?
By declaring them without the default keyword.
Given the following code, does Reptile compile?
public @interface Swimmer {
int armLength = 10;
String stroke();
String name();
}
@Swimmer(stroke=”Butterfly”, name=”Kip”, armLength=1) class Reptile { }
It doesn’t because armLength is a constant, not an element, and can thus not be included in an annotation.
What are the conditions for the use of an annotation with a value without specifying the element name?
example:
@Injured(“Broken Tail”) instead of @Injured(condition=”Broken Tail”)
The annotation declaration must contain an element names value(), which may be optional or required.
The annotation declaration must not contain any other elements that are required.
The annotation usage must not provide values for any other elements.
Does the following code compile?
public @interface Music {
String[] genres();
}
public class Reindeer {
@Music(genres={}) String favorite;
}
Yes, this compiles.
Does the following code compile?
public @interface Music {
String[] genres();
}
public class Reindeer {
@Music(genres=null) String favorite;
}
No. It cannot be null
What does the @Target ElementType value TYPE apply to?
Classes, interfaces, enums, annotations