~/ 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/e7932c93-b1e8-4e8f-8704-ad687296b92b/flashcard utf-8 LF