Showing posts with label static-initializer. Show all posts
Showing posts with label static-initializer. Show all posts

Thursday, March 8, 2012

Are Java static initializers thread safe?


I'm using a static code block to initialize some controllers in a regsitry I have. My question is therefore, can I guarantee that this static code block will only absolutely be called once when the class is first loaded? I understand I cannot guarantee when this code block will be called, Im guessing its when the Classloader first loads it. I realize I could synchronize on the class in the static code block, but my guess is this is actually what happens anyway?