Revision as of 02:24, 26 January 2006 editGary (talk | contribs)Autopatrolled, Extended confirmed users, Pending changes reviewers, Rollbackers102,842 editsNo edit summary← Previous edit | Revision as of 11:08, 11 March 2006 edit undoMTSbot~enwiki (talk | contribs)5,937 editsm robot Adding: deNext edit → | ||
Line 26: | Line 26: | ||
] | ] | ||
] | |||
] | ] | ||
] | ] |
Revision as of 11:08, 11 March 2006
In object-oriented programming, a constructor in a class is a special method (function) that can be used to create objects of the class and never has a return type. Constructors are special instance methods that are called automatically upon the creation of an object (instance of a class). They are distinguished by having the same name as the class of the object they're associated with. Its main purpose is to pre-define the object's data members and to establish the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a 'valid' state.
Java
Example
public class Example { //declaration of instance variable(s). protected int data; //definition of the constructor. public Example() { data = 1; } }
See also
References
- Bjarne Stroustrup: The C++ Programming Language, Addison-Wesley, ISBN 0-201-70073-5