This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
To take Henry's reply one step further...when exactly would that code run? It's not part of the main() method, it's not part of the test() method. It's not part of any constructor. It's just kind of hanging out somewhere, with nobody using it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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
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.
Joined: Oct 13, 2005
Gaurav Raje wrote: . . . A class is just like a class which you would attend in school. . . .
No, it isn't. The word "class" can mean "type or "sort", as well as "lesson".
Joined: Jul 23, 2010
aye aye sherriff.... you know how to create analogies better... but i hope i attempted to make a valid point