1Z0-771 study materials
1Z0-771 study resources

1Z0-771: Annotations, Reflection, and Date/Time API

1Z0-771 · Work through your exam, one topic at a time.

All guide sections

Annotations, Reflection, and Date/Time API

Domain 9 tests three APIs that frequently appear together: custom annotation definitions, reflective inspection, and the immutable java.time model.

Annotations

  • Declare with @interface; elements are methods, optionally with default values.
  • Meta-annotations: @Retention (SOURCE, CLASS the default, RUNTIME), @Target (METHOD, FIELD, TYPE, and others), @Inherited (class annotations only), @Repeatable, @Documented.
  • Only RUNTIME retention is visible to reflection through clazz.getAnnotation(MyAnno.class).

Reflection

  • Access a Class<?> via obj.getClass(), Type.class, or Class.forName("fq.Name").
  • getMethods() returns public methods including inherited ones; getDeclaredMethods() returns all methods declared on the class but none inherited.
  • setAccessible(true) bypasses access checks only when the owning module opens the package.
  • Invoking a method that throws wraps the cause in InvocationTargetException; unwrap with getCause().

Date-Time

  • LocalDate, LocalTime and LocalDateTime are zone-free and immutable; ZonedDateTime and OffsetDateTime carry a zone or offset; Instant is a UTC point on the timeline.
  • Duration is time-based (hours, seconds, nanos), Period is date-based (years, months, days). Both provide between().
  • All plus, minus and with methods return new objects. DateTimeFormatter is immutable and thread-safe, unlike SimpleDateFormat.
  • ZonedDateTime resolves daylight-saving gaps and overlaps using zone rules.

Worked example

LocalDate d = LocalDate.of(2024, 2, 28);
LocalDate e = d.plusDays(1);      // 2024-02-29, a leap year
Period p = Period.between(d, e);  // P1D

d is unchanged; plusDays returns a new instance, and leap-year handling is automatic.

Traps

  • LocalDateTime.now() ignores the zone; use ZonedDateTime.now() for zone-aware values.
  • Duration.between on two LocalDate values compiles but throws UnsupportedTemporalTypeException at runtime; use Period.
  • @Retention(SOURCE) annotations vanish from bytecode entirely.