Revision as of 19:14, 16 December 2005 editE2mb0t~enwiki (talk | contribs)376 editsm robot Adding: pt← Previous edit | Revision as of 11:18, 31 December 2005 edit undoCurvers (talk | contribs)33 editsNo edit summaryNext edit → | ||
Line 1: | Line 1: | ||
In ], a '''constructor''' is a ] |
In ], a '''constructor''' in a class is a special ] (function) that can be used to create objects of the class and never has a return type. Constructors are special ] that are called automatically upon the creation of an ] (instance of a class). They are distinguished by having the same name as the ] of the object they're associated with. Its main purpose is to pre-define the object's ] and to establish the ] of the class, failing if the invariant isn't valid. A properly written constructor will leave the ] in a 'valid' state. | ||
=== ] === | |||
==== Example ==== | |||
public class Example | |||
{ | |||
//declaration of instance ](s). | |||
protected int data; | |||
//defenition of the '''constructor'''. | |||
public Example() | |||
{ | |||
data = 1; | |||
} | |||
} | |||
==See also== | ==See also== | ||
Line 10: | Line 25: | ||
] | ] | ||
{{compu-stub}} | {{compu-stub}} | ||
Revision as of 11:18, 31 December 2005
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; //defenition of the constructor. public Example() { data = 1; } }
See also
References
- Bjarne Stroustrup: The C++ Programming Language, Addison-Wesley, ISBN 0-201-70073-5
This computing article is a stub. You can help Misplaced Pages by expanding it. |