Nullary constructor

Source: Wikipedia, the free encyclopedia.

In

arguments.[1] Also known as a 0-argument constructor, no-argument constructor,[2] parameterless constructor or default constructor.[3]

Object-oriented constructors

In

constructor is code that is run when an object is created. Default constructors of objects are usually nullary.[4]

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

  1. ^ "Default Constructor in Java – Class Constructor Example". freeCodeCamp.org. 2022-01-13. Retrieved 2022-03-23.
  2. ^ "No-argument Constructor". chortle.ccsu.edu. Retrieved 2022-03-23.
  3. ^ "Default constructors - cppreference.com". en.cppreference.com. Retrieved 2023-04-12.
  4. , retrieved 2022-03-23