FACTOID # 48: Many Americans live alone - the United States leads the world in one person households.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Constructor (computer science)

In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in Java and other object-oriented languages) or dynamically constructed on the heap through the keyword “new”. Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. ... In computer science, the object lifetime (or life cycle) of an object in object-oriented programming is the time between an objects creation (also known as instantiation or construction) till the object is no longer used, and is destructed or freed. ... To meet Wikipedias quality standards, this article or section may require cleanup. ... This article or section should be merged with Dynamic memory allocation In Heap Based Memory Allocation, the memory is allocated from a large pool of unused memory area called the heap dynamically. ...


A constructor is similar to a class method, but it differs from a method in that it never has an explicit return type, it's not inherited, and usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. Their responsibility is to initialize 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. Immutable objects must be initialized in a constructor. In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ... In object-oriented programming, a class is a programming language construct used to group related fields and methods. ... In object-oriented programming, an instance variable or data member is the data encapsulated within a class or object. ... In computer science, optimising compilers and the methodology of design by contract pay close attention to invariant quantities in computer programs, where the set of transformations involved is the execution of the steps of the computer program. ... In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete... In computer science, an immutable object, as opposed to a mutable object, is a kind of object whose internal states cannot be modified after it is created. ...


In most languages, the constructor can be overloaded in that there can be more than one constructor for a class, each having different parameters. Some languages take consideration of some special types of constructors: Method overloading is a feature found in various programming languages such as C++ and Java that allows the creation of several functions with the same name which differ from each other in terms of the type of the inbananaput and the type of the output of the function. ...

Contents

A default constructor is a constructor that can be called with no arguments. ... A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. ...

Java

Some of the differences between constructors and other Java methods: Java language redirects here. ...

  • Constructors never have an explicit return type.
  • Constructors cannot be directly invoked (the keyword “new” must be used).
  • Constructors cannot be synchronized, final, abstract, native, nor static.
  • Constructors are always executed by the same thread.

Example

 public class Example { //definition of the constructor. public Example() { data = 1; } //overloading a constructor public Example(int input) { data = input; } //declaration of instance variable(s). protected int data; } 
 //code somewhere else //instantiating an object with the above constructor Example e = new Example(42); 

In computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. ...

Visual Basic .NET

Visual Basic . ...

Example

Constructors in Visual Basic can be in one of two forms. Each form uses a regular method declaration with a special name (and no return value). The older form uses the same name as the Class itself, and the newer form uses the name "New." The newer form is the preferred one because it makes refactoring your class easier. Refactoring is the process of rewriting a computer program or other material to improve its structure or readability, while explicitly keeping its meaning or behavior. ...

 Class Foobar Private strData as String ' property data Public Property Data() As String Get Return strData End Get Set(ByVal strNewData As String) strData = strNewData End Set End Property ' Old form Sub Foobar(ByVal someParam As String ) Data = someParam End Sub ' New form Sub New(ByVal someParam As String) Data = someParam End Sub End Class 
 ' code somewhere else ' instantiating an object with the above constructor Dim foo as New Foobar(".NET") 

C#

The title given to this article is incorrect due to technical limitations. ...

Example

 class myClass { private int mA; private string mB; public myClass(int a, string b) { mA = a; mB = b; } } 
 //code somewhere //instantiating an object with the constructor above myClass c = new myClass(42, "string"); 

ColdFusion

This article or section does not adequately cite its references or sources. ...

Example

It's important to note that there is no constructor method in ColdFusion. A common practice among developers in the ColdFusion community is to create an 'init' method that acts as a pseudo-constructor.

 <cfcomponent displayname="Cheese"> <!--- properties ---> <cfset variables.cheeseName = "" /> <!--- pseudo-constructor ---> <cffunction name="init" returntype="Cheese"> <cfargument name="cheeseName" type="string" required="true" /> <cfset variables.cheeseName = arguments.cheeseName /> <cfreturn this /> </cffunction> </cfcomponent> 

PHP

For other uses, see PHP (disambiguation). ...

Example

In PHP the constructor is a method named __construct(), which is automatically called by the keyword new after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.

 class Person { private $name; function __construct($name) { $this->name = $name; } function getName() { return $this->name; } } 

Constructors simplified (with pseudocode)

Constructors are always part of the implementation of classes. A class (in programming) refers to a specification of the general characteristics of the set of objects that are members of the class rather than the specific characteristics of any object at all. A simple analogy follows. Consider the set (or class, using its generic meaning) of students at some school. Thus we have Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. ...

 class Student { // refers to the class of students // ... more omitted ... } 

However, the class Student is just a generic prototype of what a student should be. To use it, the programmer creates each student as an object or instance of the class. This object is a real quantity of data in memory whose size, layout, characteristics, and (to some extent) behavior are determined by the class definition. The usual way of creating objects is to call a constructor (classes may in general have many independent constructors). For example,

 class Student { Student (String studentName, String Address, int ID) { // ... storage of input data and other internal fields here ... } // ... 

  Results from FactBites:
 
constructor - Search Results - MSN Encarta (180 words)
Constructor provides information about, and access to, a single constructor for a class.
Constructor Magazine - A publication of the AGC
Sept-Oct 2007: Cover Story: Big Projects in the Big D Anchored by the new Dallas Cowboys stadium, Dallas has launched a full state of ambitious entertainment, hospitality and mixed...
Computer algebra system - definition of Computer algebra system in Encyclopedia (310 words)
The run-time of numerical programs implemented in computer algebra systems is normally longer than that of equivalent programs implemented in systems such as MATLAB, GNU Octave or directly in C, because the computer algebra languages are often interpreted and the bignum system may cause overhead.
Computer algebra systems began to appear in the early 1970s, and evolved out of research into artificial intelligence (the fields are now regarded as largely separate).
Some computer algebra systems focus on a specific area of application; these are typically developed in academia and free.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m