nullary constructor

{{short description|In programming, an object-creating function that takes no arguments}}

In computer programming, a nullary constructor is a constructor that takes no arguments.{{Cite web |date=2022-01-13 |title=Default Constructor in Java – Class Constructor Example |url=https://www.freecodecamp.org/news/default-constructor-in-java/ |access-date=2022-03-23 |website=freeCodeCamp.org |language=en}} Also known as a 0-argument constructor, no-argument constructor,{{Cite web |title=No-argument Constructor |url=https://chortle.ccsu.edu/java5/Notes/chap50/ch50_13.html |access-date=2022-03-23 |website=chortle.ccsu.edu}} parameterless constructor or default constructor.{{Cite web |title=Default constructors - cppreference.com |url=https://en.cppreference.com/w/cpp/language/default_constructor |access-date=2023-04-12 |website=en.cppreference.com}}

Object-oriented constructors

In object-oriented programming, a constructor is code that is run when an object is created. Default constructors of objects are usually nullary.{{Citation |last=Ottinger |first=Joseph B. |title=An Introduction to Hibernate 6 |date=2022 |url=https://doi.org/10.1007/978-1-4842-7337-1_1 |work=Beginning Hibernate 6: Java Persistence from Beginner to Pro |pages=1–25 |editor-last=Ottinger |editor-first=Joseph B. |place=Berkeley, CA |publisher=Apress |language=en |doi=10.1007/978-1-4842-7337-1_1 |isbn=978-1-4842-7337-1 |access-date=2022-03-23 |last2=Linwood |first2=Jeff |last3=Minter |first3=Dave |editor2-last=Linwood |editor2-first=Jeff |editor3-last=Minter |editor3-first=Dave|url-access=subscription }}

File:Nullary constructor UML.svg

=Java example=

public class Example

{

protected int data;

/* Nullary constructor */

public Example()

{

this(0);

}

/* Non-nullary constructor */

public Example(final int data)

{

this.data = 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

References