Jun 15, 2015

Why constructors can't be static, final or abstract?

A Java constructor is always called without an object and are not allowed to be overridden, hence is implicitly final and implicitly static. Also it is meaningless for a Java constructor to be abstract.
Actually, constructors have a mixture of static and non-static semantics. You can't "call" a constructor on an instance, and it they are not inherited, or overridable. This is similar to the way static methods work. On the other hand, the body of a constructor can refer to this, and call instance methods ... like an instance method. And then there is constructor chaining, which is unique to constructors. But the real point is that these aspects are fixed, and there is no point allowing a redundant static modifier.
This means that the final and static modifiers would be redundant, and the abstract keyword would have no meaning at all.
Naturally, the Java designers didn't see in any point in allowing redundant and/or meaningless access modifiers on constructors ... so these are not allowed by the Java grammar.
It is strange that interface methods don't share the same design call where the public  and abstractmodifiers are also redundant, but allowed anyway. Perhaps there is some (ancient) historical reason for this. But either way, it cannot be fixed without rendering (probably) millions of existing Java programs uncompilable.


Post a Comment