This is an old revision of the document!
A Constructor is a special group of Operators that are called when an Object is first created. In most Object-Oriented Programming (OOP) languages (i.e., C++, Java and C#, e.g.), the Constructor has the same name as the Object. It is the initial stage in the Lifecycle of Object Data (see 2.3.4.5 Data Lifecycle Taxonomy). For example, for an Object called Vehicle, the Constructor would also be called Vehicle. The constructor is responsible for the setup of the Object: the initialization of Field Data, the allocation of memory from the Heap. For example, if there are dynamic Field Data, then the memory needed for those Fields is usually allocated from the Heap. Although Constants are generally managed and allocated by the compiler and are from the Stack, Static Field Data can be allocated from the Heap and can be set during construction. For example, the values of Field Data initialized by using initialization parameters on the Constructor or read from initialization or setup files.
Generally, there are three kinds of constructors available in OO:
There is always a default Constructor that required no parameters, however, there can be other Constructors allowing for the passing of values to be used during initialization. For example, the minimum or maximum values used for the Field Data.
Regardless of the number of Constructors, there is always a Constructor that is called when an object is created. Often the calling of a Constructor is automatic and used the default Constructor, but the programmer can use any of the Constructors defined for the Object. See: https://www.tutorialspoint.com/solidity/solidity_constructors.htm
When Data Object is deployed in Ethereum, the following occurs:
The contract is initialized using the optional Constructor method named: constructor(). A Constructor is a special function declared using the constructor keyword. It is an optional function and is used to initialize state variables of a contract. Following are the key characteristics of a constructor.
* A contract can have only one constructor.
* A constructor code is executed once when a contract is created, and it is used to initialize contract state.
* After a constructor code executed, the final code is deployed to blockchain. This code include public functions and code reachable through public functions. Constructor code or any internal method used only by constructor are not included in final code
* A constructor can be either public or internal.
* A internal constructor marks the contract as abstract
* In case, no constructor is defined, a default constructor is present in the contract
* Note: See https://www.tutorialspoint.com/solidity/solidity_constructors.htm
<Code solidity:1 | Example of a simple Constructor>
contract Inventory
{ uint public quantityInStock;
constructor () public
{ quantityInStock = 0;
} End Inventory constructor
function checkInventory() external view
{ if ( quantityInStock < 0 )
{ revert ( "quantityInStock must be greater than 0");
} // End if
} // End checkInventory
} end Inventroy contract </Code> <Code solidity:1 | Example of a Constructor with arguments that intialize the state variables> contract Inventory { uint public quantityInStock; constructor ( uint _initialQuantity ) public { quantityInStock = _initialQuantity; } End Inventory constructor
function checkInventory() external view
{ if ( quantityInStock < 0 )
{ revert ( "quantityInStock must be greater than 0");
} // End if
} // End checkInventory
} end Inventroy contract
</Code>
===== Destructor =====
Return to Top
==== Overview ====
Return to Top
Destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following:
* Recovering Heap space allocated during the lifetime of an object (see 2.3.4.5 Data Lifecycle Taxonomy)
* Releasing Shared or Network Resources such as printers, faxes, scanners or outside Voice over Internet Protocol (VOIP) resources
* Releasing Resource Lock, for example, Web Servers, banking systems read versus update, travel reservations, etc
* Closing connections such as database, file or service
* Other housekeeping tasks
* Note: Often the first use of the Destructor, Recovering Heap memory, is automated in many modern systems (i.e., Java, .NET, Python, JavaScript, etc.) but the remaining reasons for a destructor remain in place, and it is up to the architect to determine what resources need to be freed upon the end of a Data Object (see 2.3.4.5 Data Lifecycle Taxonomy).
==== DIDO Specifics ====
Return to Top
When Data Object is destroyed in Ethereum, the following occurs:
* A contract can be destructed via the selfdestruct() method or the deprecated suicide() method
* Note: See https://www.tutorialspoint.com/solidity/solidity_constructors.htm
~~DISCUSSION:on|Outstanding Issues~~
~~DISCUSSION:off~~