Forums Register Login

Static Initializers

+Pie Number of slices to send: Send
When a subclass is instantiated, will it's parent class' static initializer run?

I simply test printing to System.out seems like the answer is no, but I'm not sure why.
+Pie Number of slices to send: Send
Static initializer code in the Parent class will execute once when the Child class is loaded (because this will also load the Parent class) -- whether or not an instance of either class is created.

Classes are loaded automatically by the JVM either when the first object of that class is created or the first time a static member of that class is accessed -- even if no object is created. (Alternatively, a class can be loaded "manually" using Class.forName(String).)

Non-static initializer code executes with each instantiation.


[ October 08, 2004: Message edited by: marc weber ]
+Pie Number of slices to send: Send
If the parent class was already loaded for one of the reasons mentioned, its static initializers won't run again when the child class is loaded. This may account for what you observed.
+Pie Number of slices to send: Send
 

Originally posted by Mike Gershman:
If the parent class was already loaded for one of the reasons mentioned, its static initializers won't run again when the child class is loaded...


Yes, that's a good point. I should have been more clear in my first paragraph. Static initializers run only once, at class load time. And if the Parent class is not already loaded, then loading the Child class will also load the Parent class.
[ October 08, 2004: Message edited by: marc weber ]
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1022 times.
Similar Threads
Access control
instance initializer
question
what compiler do & what JVM do ...
static block.......
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:07:53.