+ 1

What's static constructor and when it fires?

29th Jul 2017, 6:11 PM
Mohammad Hossein Meshkini
Mohammad Hossein Meshkini - avatar
3 Respostas
+ 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.
29th Jul 2017, 6:18 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
It uses for initiating static variables of the class and it fires just once exactly before creating first object of the class.
29th Jul 2017, 6:14 PM
Mohammad Hossein Meshkini
Mohammad Hossein Meshkini - avatar
+ 1
OK, I'm talking about C#. In the c# static constructor exists and is equivalent to SIB in JAVA.
29th Jul 2017, 6:21 PM
Mohammad Hossein Meshkini
Mohammad Hossein Meshkini - avatar