• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static and final keyword in constructors

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes rob you are right
thanks for your reply

looking for more replies
regards
amir
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic