I wondered why the size of an object ( class ) has to be known by the compiler. I mean can't it be deferred to the runtime - when the object is really needed ?
Thanks
Javac doesn't know the size of an object. The JVM abstracts away those details.
In native compiled languages like C or C++, the compiler has to know the size of an object when including it inside a larger struct or when allocating it on the stack.* In the case of heap allocations, it is possible to have a dynamically sized object, though this is very dangerous and hard to get right. In C this is traditionally done by ending the struct with an empty array and then allocating extra space.
Rust actually makes a distinction between types with known and unknown size. The later can only be used in limited circumstances. C/C++ has a similar notion with incomplete types, that are basically only usable as opaque pointers.
* Technically C does allow variable sized arrays on the stack.