Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
m robot Adding: pt
Curvers (talk | contribs)
No edit summary
Line 1:
In [[object-oriented programming]], a '''constructor''' in a class is a special [[method (computer science)|method]] which(function) isthat automaticallycan invokedbe whenused to create objects of the class and never has a return type. Constructors are special [[method (computer science)|instance methods]] that are called automatically upon the creation of an [[object (computer science)|object]] is(instance createdof a class). They are distinguished by having the same name as the [[class (computer science)|class]] of the object they're associated with. Its main purpose is to pre-define the object's [[data member|data members]] and to establish the [[invariant (computer science)|invariant]] of the class, failing if the invariant isn't valid. A properly written constructor will leave the [[object (computer science)|object]] in a 'valid' state.
 
=== [[Java programming language|Java]] ===
==== Example ====
public class Example
{
//declaration of instance [[variable (computer science)|variable]](s).
protected int data;
//defenition of the '''constructor'''.
public Example()
{
data = 1;
}
}
 
==See also==
Line 10 ⟶ 25:
 
[[category:Object-oriented programming]]
 
{{compu-stub}}