+ 5

Why does C# have static constructors whereas Java does not?

It seems that the Java alternative to this is static initialisation blocks and the C++ way is to have static inner classes. The purpose of all these seem to be the initialisation of static members of a class. Is my understanding correct?

6th Apr 2019, 11:36 PM
Sonic
Sonic - avatar
2 odpowiedzi
+ 7
I think this also helpful: https://stackoverflow.com/questions/4506990/what-is-the-use-of-static-constructors "A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced."
6th Apr 2019, 11:53 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
I don't know why c# has static constructors but here is an explanation why java can not have static constructors: https://beginnersbook.com/2013/05/static-constructor/
6th Apr 2019, 11:44 PM
Denise Roßberg
Denise Roßberg - avatar