java - Why does StructuredArray need to be non-constructible? -


this talk @ 34:00 describes design of structuredarrays java. everything's rather clear, except on thing:

it shouldn't constructible, i.e., instance may obtainable static factory method newinstance. @ same time, should subclassible, means there must public constructor , non-constructibility assured @ runtime. sounds hacky, wonder why?

i'm aware advantages of factories in general , static factory methods in particular. here, makes hack acceptable?

the point of structuredarray class someday can replaced intrinsic implementation allocates whole array, including component objects, 1 long block of memory. when happens, size of object depend on number of elements , element class.

if structuredarray had public constructor, write x = new structuredarray<>(structuredarray.class, myelement.class, length). doesn't seem present problem, except in bytecode, turns new instruction allocates object, , separate invokespecial instruction call object's constructor.

you see problem -- new instruction has allocate object, cannot, because size of object depends on constructor parameters (the element class , length) doesn't have! aren't passed until constructor call follows sometime later.

there ways around problems this, they're kinda gross. makes lot more sense encapsulate construction in static factory method, because can't write new structuredarray..., , jvm doesn't have use "magic" figure out how memory allocate in new instruction structuredarray, because there can't such instructions*.

if later jvm wants provide intrinsic implementation of static factory allocates contiguous array, it's no problem -- gets information needs in factory method invocation.

nb* - yes, ok, technically can write new structuredarray..., doesn't make useful object you.


Comments