Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of static constructors?

Please explain to me the use of static constructor. Why and when would we create a static constructor and is it possible to overload one?

like image 906
Dr. Rajesh Rolen Avatar asked Dec 22 '10 07:12

Dr. Rajesh Rolen


People also ask

What is the use of static constructor in Java?

A static constructor used to initialize static data means the specified task will execute only once throughout the program. Usually, a static constructor is automatically called when the first instance is generated, or any static member is referenced.

Why do we use static constructors Mcq?

8. Why do we use static constructors? Explanation: Static constructors help in initializing the static members of the class. This is provided because the static members are not considered to be property of the object, rather they are considered as the property of class.

Can we use static constructor in Java?

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur. In general, static means class level. A constructor will be used to assign initial values for the instance variables.

What is the use of static constructor in non static class?

Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class.


2 Answers

No you can't overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.

It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to run at most once (even if two threads arrive at the same time).

like image 187
Marc Gravell Avatar answered Sep 19 '22 13:09

Marc Gravell


From Static Constructors (C# Programming Guide):

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.

Static constructors have the following properties:

  • A static constructor does not take access modifiers or have parameters.

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

  • A static constructor cannot be called directly.

  • The user has no control on when the static constructor is executed in the program.

  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

like image 30
Adriaan Stander Avatar answered Sep 22 '22 13:09

Adriaan Stander