Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Final ?

 
Anupam Bhatt
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 ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 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?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
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 !
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • 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!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
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.
 
Ilja Preuss
author
Sheriff
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic