Coming from Java where Static Block are immediately called. In VB.NET (ASP.NET) a Module Constructor isn't called until the first method is called. So, this begs the question, if I'm performing initialization within my Module's constructor do I need to wrap it in a Synclock?
Asked
Active
Viewed 175 times
1 Answers
0
Modules are a VB.NET programming nicety. Once compiled, they are the same as C# static classes, therefore a module constructor is the same as a C# static constructor. A C# static constructor is presumably exactly the same as a static block in Java. The documentation states that a C# static constructor is never executed more than once, so that would suggest that no synchronisation is necessary.

jmcilhinney
- 50,448
- 5
- 26
- 46
-
Yes, I thought so. I read the following article before posting the question: http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx and the following paragraph caused some confusion (near bottom, Example): "the class Bus has a static constructor and one static member, Drive(). When Drive() is called, the static constructor is invoked to initialize the class." -- Hence leading to the notion that maybe the static constructor doesn't get called until one of it's methods are called first. – Oct 01 '14 at 06:51
-
A static constructor doesn't get invoked until the class is used. Usually, that means before the instance constructor when the first instance is created. I imagine that it's the same in Java. In the case of a static class though, there is no instance constructor and there is no instance, so the first time a class is used is when a static member is first invoked. The static constructor will be implicitly invoked before that member. – jmcilhinney Oct 01 '14 at 06:55
-
In Java a Static Block is called the instant the Class Loader loads the class into memory, regardless if its ever used or not. Its a cool feature of Tomcat, it ensures all static classes are initialized and ready before they are called. I just did a test and you're right, the static constructor isn't executed until the static class is used. Appreciate the clarification. – Oct 01 '14 at 07:01