Why constructors cannot be final

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

For example, can you explain to me why this is not valid?

public class K { abstract public K() { // ... } } 
+62
java syntax constructor
Feb 28 '12 at 6:35
source share
10 answers

When you set the method to final , it means: "You do not want any class to override it." But the constructor (according to the Java Language Specification) cannot be overridden, so it is clean.

When you set a method as abstract , it means: "The method has no body and must be implemented in a child class." But the constructor is called implicitly when the new keyword is used, so it cannot lose its body.

When you set a method as static , it means: "The method belongs to a class, not a specific object." But the constructor is implicitly called to initialize the object, so there is no purpose in creating a static constructor.

+133
Feb 28 2018-12-12T00:
source share

The question is why you want the constructor to be static or abstract or final .

Constructors are not inherited, therefore they cannot be redefined, therefore use to have a finite constructor

The constructor is called automatically when the class instance has access to the fields of the class instance. What will be the use of a static constructor.

The constructor cannot be overridden, so what you will do with the abstract constructor.

+36
Feb 28 '12 at 6:40
source share

The Java constructor is implicitly finite and implicitly static 1 and the Java constructor does not make sense to be abstract.

This means that the final and static modifiers will be redundant, and the abstract keyword does not make sense at all.

Naturally, Java developers did not see anything in resolving redundant and / or meaningless access modifiers for constructors ... therefore, they are not allowed using Java grammar.

In addition: it is a shame that they did not make the same design call for interface methods, where the public and abstract modifiers are also redundant, but allowed in any case. Perhaps there is some (ancient) historical reason for this. But in any case, it cannot be fixed without rendering (possibly) millions of existing incompatible Java programs.




1 - In fact, constructors have a mixture of static and non-static semantics. You cannot “call” a constructor in an instance, and it is not inherited or redefined. This is similar to how static methods work. On the other hand, the constructor body can refer to this and call instance methods ... as an instance method. And then there is a chain of designers that is unique to designers. But the real point is that these aspects are fixed, and it makes no sense to allow the redundant static modifier.

+14
Feb 28 '12 at 7:21
source share
  • public constructor . Objects can be created anywhere.

  • default constructor . Objects can be created in only one package.

  • protected constructor . Objects can be created by classes outside the package only if it is a subclass.

  • private constructor . An object can only be created inside a class (for example, when implementing a singleton).

The keywords static , final and abstract do not make sense to the constructor because:

  • static members belong to the class, but a constructor is needed to create the object.

  • The abstract class is a partially implemented class that contains abstract methods that must be implemented in a child class.

  • final restricts the modification: variables become constant, methods cannot be overridden, and classes cannot be inherited.

+8
Mar 08 '14 at 9:32
source share

Final : since you still cannot overwrite / extend the constructor. You can expand the class (so that you don't make it final) or overwrite the method (so you don't make it final), but there is nothing like that for constructors.

Static If you look at execution, the constructor is not static (it can access the instance fields), if you look at the caller side, it is (type) static (you call it without with the instance. It is hard to imagine that the constructor is completely static or not it is static and has no semantic separation between these two things, so it makes no sense to distinguish them from a modifier.

Annotation . The abstract makes sense only if there is a rewrite / extension, therefore the same argument is used as for "final".

+6
Feb 28 2018-12-12T00:
source share

No constructors NEVER can be declared final. Your compiler will always give an error like "final final final final" The final applied to the methods means that the method cannot be overridden in a subclass. Constructors are NOT normal methods. (different rules apply) In addition, constructors are NEVER inherited. Thus, in the announcement of the final answer there are NO CASES.

+5
Jul 30 '15 at 11:44
source share
  • Constructors are NOT normal methods. (different rules apply)
  • In addition, designers are NEVER inherited. Thus, the announcement of the final is not a few. Constructors can never be declared final. Your compiler will always give an error like "end of modifier is not allowed"
  • Check out the JLS section 8.8.3 (JLS documents and APIs should be some of your primary sources of information).
+3
Jan 08 '16 at 6:46
source share

JLS Section 8 mentions this.

Constructors (§8.8) are similar to methods , but cannot be called directly by calling a method; they are used to initialize a new instance class. Similar methods may be overloaded (§8.8.8).

But the designers in the calculation are not regular methods. They cannot be compared as such.

+2
Feb 28 2018-12-12T00:
source share

why a constructor cannot be static and final are clearly defined in the answers above.

Abstract: "Abstract" means the lack of implementation. and it can only be implemented through inheritance. Therefore, when we extend some class, all members of the parent class are inherited in the subclass (child class), except for "Constructor". So, let's suppose you can somehow declare the Abstract constructor, than how can you give its implementation in a subclass when the constructor does not receive inheritance in the child class?

why the constructor cannot be abstract.

+2
Mar 08 '14 at 8:00
source share

allows you to see the first final publication K () {

* It’s limited over the final modifier, because if it’s final, then there’s a certain situation when we redefine it in some other class or in the same class so that it doesn’t happen here, for example:

 we want public void(int i,String name){ //this code not allowed 

let static, static itz is all about the class level, but we create an object-based constructor using the 'new' keyword, therefore ,,, thatsall

itz abstraction is not the worst about here because it has no abstract method or any method declared

+1
May 31 '15 at 19:22
source share



All Articles