Chapter 13: Annotations Flashcards
What is metadata?
Metadata is data that provides information about other data.
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 effect of annotations during program compilation?
There is none. Annotations can be removed and it will still compile and run, albeit with different behaviour.
What is the simplest form to define an annotation?
public @interface AnnotationName { }
What is an annotation definition that doesnt contain any elements also called?
A marker annotation.
Is the following code valid?
public @interface Exercise { int hoursPerDay(); }
@Exercise public class ZooEmployee { }
It is not valid and will not compile because the hoursPerDay field is required and must be assigned. (It is required because it contains no default value).
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.
Is the following code valid?
public @interface Exercise { int hoursPerDay(); int startHour() default 6; }
@Exercise(hoursPerDay = 3) public class ZooEmployee { }
This code is valid.
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=) String favorite; }
No, genres must be assigned.
Does the following code compile?
public @interface Music {
String[] genres();
}
public class Reindeer { @Music(genres=null) String favorite; }
No. It cannot be null