Most programming languages, such as C / C++, Java, and Javascript, include the new
keyword in their syntax. The basic aim of the new
keyword is to create the instance of any user-defined data type, along with the allocation of memory and many other actions depending upon different languages.
new
keyword in SolidityThe new
keyword in solidity is used to instantiate a new smart contract. In other words, when a smart contract has to create another smart contract, it is done using the new
keyword.
The complete code of the contract which will be invoked by new
must be known at the compile time of the contract which will create it. Interaction with another contract involves risk, especially if the contract's source code is unknown beforehand. The existing contract transfers authority to the called contract, which could potentially do anything. For example, a calling contract can invoke functions of the system, functions of any other contract, or functions of the calling contract before even the current call is returned. This means that the calling contract may also change the values of state variables of the current contract via its public functions.
Contract_Name variableName = new Contract_Name (arguments);/* Note:If there would be any arguments we will write them in bracketse.g: Contract_Name variableName = new Contract_Name(arguments);else we will leave brackets emptye.g: Contract_Name variableName = new Contract_Name();*/
The initialization of a smart contract using the new
keyword results in:
Creation of instance of a new contract.
Initialization of the state variables.
Execution of the constructor of the new smart contract.
Assignment of
Return of the address of the newly created smart contract to the caller contract.
pragma solidity ^0.5.0;contract Student {string private stdName;uint private stdRollNum;// setting values of state variablesconstructor (string memory name,uint rollNum) public {stdName = name;stdRollNum = rollNum;}}contract StudentsList {// array to store students ( composition )Student[] private students;constructor () public{// initializing new student using `new` keywordStudent newStudent = new Student("Burak", 20);// pushing into students arraystudents.push(newStudent);}}
Line 1: We indicate which version of the compiler is compatible to run the source code. In our case, the program will run on Solidity version 0.5 and above.
Line 3: We create a Student
.
Line 4: We declare a private stdName
, referred to the student's name.
Line 5: We declare another stdRollNum
referred to the student's roll number.
Line 8 – 12: We declare an name
and rollNum
as arguments that are assigned to stdName
and stdRollNum
respectively.
Line 15: We create another smart contract StudentsList
which has a list of the type Student
as a state variable.
Line 17: We declare an array of type Student
referred to students
as a private state variable of contract StudentsList
.
Line 19 – 25: We define the default constructor with the constructor
keyword, to create an instance of the smart contract Student
. At the time of instantiation:
An instance of Student
will be created, referred as newStudent
.
Student smart contract will be deployed on the blockchain.
State variables stdName
and stdRollNum
will be initialized.
The constructor of Student
will be executed.
The address of the new smart contract Student
will be returned to the StudentList
contract.
Line 21: We initialize newStudent
by passing parameters Burak
and 20
respectively.
Line 24: We add newStudent
to students
the array.
On the creation of a new contract, the transaction is charged with a specific amount of gas which has to be paid by the caller of the contract. For sending gas at the time of creating an instance of a new smart contract line 19 will be written as:
Line 19: Student newStudent = new Student {value: amount }("Burak", 20);
An out-of-gas exception will be thrown if the gas is used up at any point that it would be negative, and eventually, the execution of the code will be stopped. Moreover, all of the manipulations will be reversed. It is to be noted that the already-used gas will not be refunded.
Free Resources