~/ learn/ comp-308/ cards/ Reflection: Constructor, Method & Field
1 of 7

Type the reflective Event factory body

Type the reflective Event factory body

Answer

Class<?> c = Class.forName(name); Constructor<?> ctor = c.getConstructor(long.class); return (Event) ctor.newInstance(time);

Class.forName loads the class by name; getConstructor(long.class) finds the public Event(long) constructor; newInstance(time) calls it and the result is cast to Event. This is the exact pattern the Greenhouse Part-2 event factory uses to add events without recompiling.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/d0b6d2d3-bbfa-469d-94d8-2496fdc76d9c/flashcard utf-8 LF