• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Constructor cannot be overridden. Then in that case why can't they be made final??

2. Since constructors are created at class level, why can't constructor be static ?

Thanks
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the point of a static constructor? Why would you want to run a constructor, usually used to set the initial value of instance variables, without an instance of a class?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W. Joe Smith wrote:What would be the point of a static constructor? Why would you want to run a constructor, usually used to set the initial value of instance variables, without an instance of a class?

A constructor is called before an instance is created. Therefore you could say it is always static. In fact, in Delphi, you create objects by calling the constructor as if it were a static method:
The reason you can't add these keywords is probably simple: why would you need them? Would it make a difference is you added them? No it wouldn't. So to avoid confusion, they are simply forbidden.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ask me would it make a difference ? I would say yes it makes a difference. In java anything that can't be inherited/overridden is made final. Then in that case since constructor cannot be overridden why not make them final ???

However final modifier cannot be applied to Java. I would like to get some better explaination for this . Thanks in advance.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please respond to my question...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a constructor is not a method - it's a special block of code that's executed to initialize a new instance of a class. So the rules that apply to normal methods are not necessarily applicable to constructors.

As the others have already said, modifiers such as static and final simply have no meaning for constructors, and it doesn't make sense to use those modifiers on constructors. If it would be allowed to use those modifiers, it would only be confusing - then people would be asking themselves why you can use those modifiers, while they don't mean anything.

Why do you insist on having the possibility of using static and final on constructors when you already know it wouldn't be useful? What would be the difference between a constructor that is static or final and a constructor that is not static or final?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!