Misplaced Pages

Constructor (object-oriented programming): Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 11:18, 31 December 2005 editCurvers (talk | contribs)33 editsNo edit summary← Previous edit Revision as of 15:52, 15 January 2006 edit undo86.42.11.23 (talk) ExampleNext edit →
Line 9: Line 9:
protected int data; protected int data;
//defenition of the '''constructor'''. //definition of the '''constructor'''.
public Example() public Example()
{ {

Revision as of 15:52, 15 January 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

Stub icon

This computing article is a stub. You can help Misplaced Pages by expanding it.

Categories: