It's not a secret anymore!
The moose likes Beginning Java and the fly likes Constructor Final ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor Final ?" Watch "Constructor Final ?" New topic

Constructor Final ?

Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 20, 2006
Posts: 3381

As such the constructors are not inherited, there is no sense/meaning to keep them as 'final'.

This link may help you!

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
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

Joined: Oct 13, 2005
Posts: 46424
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

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Campbell Ritchie:
Isn't "Doesn't make sense" a good reason for it?

Not for private methods, obviously...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Constructor Final ?
It's not a secret anymore!