Why can't constructors be final, static, or abstract?
Why can't constructors be final, static, or abstract in Java?
For instance, can you explain to me why this is not valid?
public class K {
abstract public K() {
// ...
}
}
When you set a method as final
it means: "I don't want any class override it." But according to the Java Language Specification:
JLS 8.8 - "Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding."
When you set a method as abstract
it means: "This method doesn't have a body and it should be implemented in a child class." But the constructor is called implicitly when the new
keyword is used so it can't lack a body.
When you set a method as static
it means: "This method belongs to the class, not a particular object." But the constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor.
The question really is why you want constructor to be static or abstract or final
.
Constructors aren't inherited so can't be overridden so whats the use to have final constructor
Constructor is called automatically when an instance of the class is created, it has access to instance fields of the class. What will be the use of a static constructor.
Constructor can't be overridden so what will you do with an abstract constructor.
A Java constructor is implicitly final, the static / non-static aspects of its semantics are implicit1, and it is meaningless for a Java constructor to be abstract.
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.
Aside: It is a shame that they didn't make the same design call for interface methods where the public
and abstract
modifiers 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.
1 - 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 semantics are fixed, and there is no point allowing a redundant and probably confusing static
modifier.
public
constructor: Objects can be created anywhere.default constructor: Objects can be created only in the same package.
protected
constructor: Objects can be created by classes outside the package only if it's a subclass.private
constructor: Object can only be created inside the class (e.g., when implementing a singleton).
The static
, final
and abstract
keywords are not meaningful for a constructor because:
static
members belong to a class, but the constructor is needed to create an object.An
abstract
class is a partially implemented class, which contains abstract methods to be implemented in child class.final
restricts modification: variables become constant, methods can't be overridden, and classes can't be inherited.