Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static and final keyword in constructors  RSS feed

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

why static and final keywords are not allowed for
a constructor in class ,but a method in a class may be static or final
i have read in book that constructors are similar to methods without
return type

looking for your reply's
amir
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor makes an instance of the exact class in which it is declared. A constructor can never be overridden.

The "static" keyword means "not associated with any instance". So what would a static constructor do?

The "final" keyword means "do not allow overriding". So what would it mean for a constructor?
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well peter

iam thinking about how far java is flexible and robust

if final keyword is allowed for a constructor hardly no class
will have more than one constructor if i think one constructor is enough for
a class then i may have a final keyword for a constructor

why not a constructor final?

but with the static constructor you can override the the constuctor( with diffent arguments).
constructor is just a method without return type these methods similar to factory methods
which produces objects

if we are able to create a static variable for a class then why we are not able
to create static constructor


this is my thought please correct me if iam wrong

regards
amir
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amirtharaj Chinnaraj:
if final keyword is allowed for a constructor hardly no class
will have more than one constructor if i think one constructor is enough for a class then i may have a final keyword for a constructor

why not a constructor final?

Excuse me? There are hundreds of classes with multiple constructors, and several of those need them for sake of simplicity. Or do you always want to fill in 5+ fields when only one is enough, and the rest get default values?

Also, even with final, you can still overload. It is possible to overload final methods without any problems.

As for your question, it couldn't harm if a constructor would be allowed to be marked as final. It doesn't make any sense as nothing can overwrite a constructor anyway, but it could be legal.
[ October 30, 2007: Message edited by: Rob Prime ]
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes rob you are right
thanks for your reply

looking for more replies
regards
amir
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are used to initialize a particular instance of class so it cant be static,
Constructors are never overridden so final doesn't make any sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!