+ 1
What's static constructor and when it fires?
3 Réponses
+ 2
You should really include the language you're referring to in the tag of the question.
I'll just assume you're talking about Java since you are in that course.
Constructors are meant for instantiation of a class.
Therefore, a constructor CANNOT be static.
However, Java has something called a static block, or SIB (Static Initializer Block).
The SIB is called once when the class is loaded. Because it is static it can only be used for other static calls/initializations.
+ 1
It uses for initiating static variables of the class and it fires just once exactly before creating first object of the class.
+ 1
OK, I'm talking about C#. In the c# static constructor exists and is equivalent to SIB in JAVA.