I am hearing from another developer that an object is too expensive to instantiate repeatedly because “it has a bunch of methods.”
My understanding (from Bloch, mostly) was that object creation is costly mostly through things done explicitly in the constructor, especially creating other expensive objects.
Is there a per-method cost for a new object in Java? I’m thinking not, but I need references if anyone has them.
Thanks!
Many methods means a big virtual method table (VMT). However, the VMT is per class just like metadata and therefore does only have at most a one-time cost on the very first instantiation. Subsequent instantiations are just as fast as objects with less methods, assuming that the constructor(s) do not do heavy lifting.
Worth a read is also the chapter on object creation from the performance tuning book.