Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor - Which are the difference between methods and constructors?

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor - Which are the difference between methods and constructors? Give me an
exemple please...

André AS
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors run any time a class is created and don't have a return type.

I recommend reading this.
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much...
André AS
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of the constructor is exactly the same as the name of the class. Remember: no return type.
 
Danish Sualeh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add , constructors should really be concerned with initializing a class instance.

Doing too much work in the constructor may cause problems down the line such as when you want to unit test your code , etc.

In such cases , work should be delegated to methods
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic