Post a Job  |   Ask a Question?   |   Already registered? login or register
Vivek Tiwari
18/11/2016 11:04 PM

Core Java

Did we have static constructors in java? What is purpose of it?
Spam    4 Comments

18/11/2016 23:08:13
A constructor is used to initialize the instance of an object so it must have a reference to the instance. Constructor is static. I repeat Constructor in Java (programming language) is STATIC. Reason that you can use non-static variables in Constructor, is in use of Constructor.

18/11/2016 23:08:41
Let's assume, Constructor is Non static. To use that constructor in static main method, we need instance of that class. But we need constructor to initialize instance/Object... A Deadlock condition will occur. Therefore they made constructor STATIC.

18/11/2016 23:15:17
Then why it is not compiling?

23/11/2016 11:28:34
We can not write static modifier for the Constructor, 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.

Languages :