Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Good Citizenship > Constructors

Constructors

In most modern object-oriented languages, we take constructors for granted. We think of them merely as the mechanism used to create new objects. But constructors have a more noble purpose: they tell you what it takes to create a valid object of a certain type. Constructors form a contractual relationship with the consumers of an object, indicating what fields must be populated in order to have a valid object of this type.

Unfortunately, the authorities in the language worlds advocate against having meaningful constructors. Most languages virtually insist that all classes have a default constructor (that is, one that takes no parameters). From a citizenship standpoint, this doesn’t make any sense. How many times have you heard one of the business people say, “We need to ship widgets to this customer, but we don’t have any information about it.” You can’t ship things to customers that have no internal state. Objects are the keepers of state, and having an object with no state doesn’t make sense. Virtually every object should start out with at least some minimal initial state. Is it possib....


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint