~/ learn/ comp-308/ cards/ Part 2 recap: reflection-built events & thread-safe concurrency
1 of 8

Type the reflective construction of an inner Event by name

Type the reflective construction of an inner Event by name

Answer

Class<?> c = Class.forName("GreenhouseControls$" + name); Constructor<?> ctor = c.getConstructor(GreenhouseControls.class, long.class); Event e = (Event) ctor.newInstance(outer, time);

Class.forName loads by the binary name (outer$inner). An inner class ctor’s first parameter is the enclosing instance, so getConstructor lists GreenhouseControls.class first and newInstance passes `outer` first. No controller recompile is needed to add an event type.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/27f5163f-390e-4c37-893d-a9e195435bba/flashcard utf-8 LF