+ 2

Can a Java Constructor be made final?

public class Blueprint{ final public Blueprint(){} }// is the use of the final keyword valid in this Java code?

14th Mar 2020, 6:42 AM
Michael Solomon
Michael Solomon - avatar
7 Respuestas
+ 6
Constructors aren't inherited so can't be overridden so their is no use of having final constructor in Java ! When you don't want any method or function to be overridden than we make use the final constructor but constructor are by default from JLS (Java Language Specification) definition can’t be overridden, so there is no final constructor is present in java.
14th Mar 2020, 6:54 AM
DishaAhuja
DishaAhuja - avatar
+ 6
No constructor cannot be made final or other modifiers accept public, private, protected, default.
21st Mar 2020, 1:37 AM
Ketul Patel
Ketul Patel - avatar
+ 4
What do you get if you try to compile it.
15th Mar 2020, 1:15 PM
Sonic
Sonic - avatar
+ 3
All aspiring Java Developers and present Java Developers are welcome to answer this question.
14th Mar 2020, 6:44 AM
Michael Solomon
Michael Solomon - avatar
+ 3
There are even other non-access modifiers that cannot be used with constructors. https://www.javaworld.com/javaworld/jw-10-2000/jw-1013-constructors.html
14th Mar 2020, 7:51 AM
Avinesh
Avinesh - avatar
+ 3
You are all awesome! Knowledge truly unlocks the door to greatness. Thanks for your brilliant answers DishaAhuja & Avinesh
14th Mar 2020, 8:26 AM
Michael Solomon
Michael Solomon - avatar
+ 3
A constructor gets invoked when a new object is created. And the answer is no. A constructor cannot be made final for the reasons DishaAhuja and Avinesh gave.
14th Mar 2020, 8:33 AM
Michael Solomon
Michael Solomon - avatar