• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor  RSS feed

 
Mrs.Noorulain faisal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the main uses of constructors :?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make sure things are 'set up' right in your object. Each time an object is created, one of it's constructors is called. You can enforce data integrity, such as a person's age being between 0 and 120. You can use it to open connections to files/databases/whatever. you can use it to send messages saying 'such and such an object was created.

Basically, anything that has to be done before the object is 'ready' is done there.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!