+ 1

Why java doesn't support static constructor , it's true for c#?

4th Jun 2018, 2:38 PM
<ajay/>$dabhi
<ajay/>$dabhi - avatar
4 Answers
+ 5
In Java simply use a static block or better: use a static factory method, that calls a private constructor, see Effective Java by Joshua Bloch ;)
4th Jun 2018, 3:23 PM
Tashi N
Tashi N - avatar
+ 4
static method cannot be inherited in the sub class because they belong to the class in which they have been declared.  since each constructor is being called by its subclass during creation of the object of its subclass, so if you mark constructor as static the subclass will not be able to access the constructor of its parent class because it is marked static and thus belong to the class only. This will violate the whole purpose of inheritance concept and that is reason why a constructor cannot be static.
4th Jun 2018, 2:50 PM
MsJ
MsJ - avatar
+ 2
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. We have no control on when the static constructor is executed in the program.
4th Jun 2018, 2:57 PM
MsJ
MsJ - avatar
+ 1
If we define static constructor in c# then?
4th Jun 2018, 2:54 PM
<ajay/>$dabhi
<ajay/>$dabhi - avatar