Advanced Features in Java: Using Custom Annotations

Expected Duration
Lesson Objectives
Course Number
Expertise Level


In this course, you'll learn about two advanced built-in annotations, @SafeVarargs and @FunctionalInterface, before moving on to defining and using your own custom annotations. You'll begin with @SafeVarargs and see that it is purely indicative and does not imply any added compiler checks, so it is important to not be lulled into a false sense of security about methods that have been decorated with this annotation. Next, you'll move to another built-in annotation, @FunctionalInterface. This is used to decorate interfaces that contain exactly one abstract method. Finally, you'll learn how to define and use custom annotations. This will involve a detailed study of target and retention policies. The retention policy determines whether the annotation will be preserved only in source code, into compiled bytecode, or all the way to runtime. Finally, you'll see how the target policy governs what code elements - fields, methods, constructors, type parameters, and classes - can be decorated with an annotation.

Expected Duration (hours)

Lesson Objectives

Advanced Features in Java: Using Custom Annotations

  • discover the key concepts covered in this course
  • describe how varargs are defined and used
  • demonstrate that the @SafeVarargs annotation is purely indicative and does not perform any run-time or compile-time checks
  • recognize how functional interfaces must contain exactly one abstract method and what qualifies as an abstract method in this context
  • recognize exactly what counts as a functional interface and what does not
  • describe the use of custom annotations and recognize that these are usually purely intended to be understood by programmers and that the standard Java compiler does not understand them
  • change the target policy to control exactly what code elements an annotation can be applied to
  • ensure that all the elements marked with an annotation satisfy certain conditions
  • demonstrate the use of named elements in an annotation
  • apply default values to an element in annotation and also experiment with unnamed elements
  • identify how to use an annotation with a type parameter
  • recognize how annotations can be used with target policy Type_Use
  • summarize the key concepts covered in this course
  • Course Number:

    Expertise Level