to take it further (and pretend i have profound knowledge
)
Always remember, classes are abstract entities(not to be confused with abstract classes). They are not supposed to execute any code or have sideeffects. I am not sayint they dont, but they are not supposed to. So this is one of the reason, most of the executable statements are tried to be avoided.
A class is just like a class which you would attend in school. Makes no sense, has nothing in real life except a schedule attached on the college website. It becomes real only if at least one student attends it. If no one attends, it should be as if the class never existed. Just like a good college will make sure thie resources which were to be utilized by this class were given to other classes, a good VM will do exactly the same. In such cases, construction side effects will create problems
Theoretically speaking
Always remember, declarations are memoryless statements. Definitions are memory statements. Even though internally they are executables, from the outside perspective, they just assign values. They have no strict rule about when they are initialized. All that is guaranteed is that they will be initialized before you actually need to use them.
In order to guarantee this,
java has stated in its specifications, when the class will be loaded(and thus when the static fields will be initialized)> But this is not what a good programmer should rely on.
You should thank god that java has such a specification. There are some languages which have neither.
As a programmer,
1) all executables have to be enclosed inside method bodies.
2) if you perform complex logic for initializing fields, it makes sense to put them in constructors or static initializer blocks(for static fields)
All said and done... as a conflicting argument
1) Declarations do take up space albeit very small(computer just doesnt remember what names variables have or stuff)... but this is taken care of by the linker(or prolly JVM in this case... wil edit if some language guru tells me... bin a while since i did this)
2) Even declarations are executable statements... but only with respect to the compiler. Not for us. Again this is hidden away from us.