Nullary constructor
Encyclopedia
In computer programming, a nullary constructor is a constructor
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...

 that takes no argument
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

s.

Object-oriented constructors

In object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

, a constructor
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...

 is code that is run when an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 is created. Default constructor
Default constructor
In computer programming languages the term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors ; this automatically provided constructor is usually a nullary constructor...

s of objects are usually nullary.

Java example


public class Example
{
/* nullary constructor */
public Example
{
this(1);
}

/* non-nullary constructor */
public Example (int data)
{
this.data = data;
}

protected int data;
}

Algebraic data types

In algebraic data types, a constructor is one of many tags that wrap data. If a constructor does not take any data arguments, it is nullary.

Haskell example


-- nullary type constructor with two nullary data constructors
data Bool = False
| True

-- non-nullary type constructor with one non-nullary data constructor
data Point a = Point a a

-- non-nullary type constructor with...
data Maybe a = Nothing -- ...nullary data constructor
| Just a -- ...unary data constructor
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK