• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Constructor Final ?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Again,

"A constructor cannot be abstract, static, final, native, or synchronized."

I understand on why it can't be all of the above, except "final".
why can't we have a final constructor, i understand constructors are not inherited, hence no chance/case of overriding etc. But why is it not allowed at all ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anupam,

i am not sure.. but i tell my Openion,,,

evertime object created ,then constructor will run right?

if every time constructor run means it is opposed to final

// any update on Anupam Query?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As such the constructors are not inherited, there is no sense/meaning to keep them as 'final'.

This link may help you! http://forum.java.sun.com/thread.jspa?threadID=377567&messageID=1612660
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Raghavan and seetharaman for the reply.

I understand constructors can not be inherited etc., why i had asked this question was to ask this itself, is the reason only "doesn't make sense to be final" or is there some design/OOPS implications of allowing constructors to be final.

Looks like the only reason is "doesn't make sense to be final". Doesn't satisfy my quest though ! sigh !
 
Marshal
Posts: 70308
283
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Anupam Bhatt:
"doesn't make sense to be final"

Isn't "Doesn't make sense" a good reason for it?

There are few enough things one can enforce in the compiler, but the more restrictions there are against compiling stupid things, the less chance there is for things to go wrong!
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructors are not inherited, by definition.

'final' means 'don't let this be inherited'.

So, while you technically COULD allow it, it would cause more confusion. Somebody would come along and say "THIS constructor is final, but THAT one isn't... what's the difference?" and spend time trying to figure it out.
 
author
Posts: 14112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Isn't "Doesn't make sense" a good reason for it?



Not for private methods, obviously...
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic