Isn't there a way to ensure that NamedSPILoader would finish before forName() is accessed?
Unfortunately not, this is a chicken-and-egg problem: If you access Codec.class in any way (e.g. already at the time when you subclass it!), it is classloaded. After classloaded, it is initialized, so its static <clinit> method is called. This method runs new NamedSPILoader(Codec.class) [the first line in the class source code], and this one loads all codecs which are listed in META-INF/services. As e.g. AppendingCodec is listed there, it is loaded by NamedSPILoader and its ctor is called. As we are still in the stacktrace inside NamedSPILoader.<init>, this call did not yet return to caller (Codec.<clinit>), so the assignment of the static final loader field is not yet finished. Unfortunately, AppendingCodec.<init> calls Codec.forName() and hits the NPE, because the field is not yet initialized. You have to say: You cannot call any method of Codec at that time!