How to use the new keyword in Solidity
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 Solidity
The 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.
Prerequisite
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.
Syntax
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();*/
Result
The initialization of a smart contract using the new keyword results in:
Creation of instance of a new contract.
Deployment of the new smart contract on the blockchain.A contract having (composed of) the instance of other contract as state variable Initialization of the state variables.
Execution of the constructor of the new smart contract.
Assignment of
value to one.nonce `number used once` is increased by 1 after every transaction Return of the address of the newly created smart contract to the caller contract.
Code
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);}}
Code explanation
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
namedsmart contract A smart contract is a collection of state variables and functions to manipulate the state variables. Student.Line 4: We declare a private
state variable Variables whose values are permanently stored in a contract storage. stdName, referred to the student's name.Line 5: We declare another
state variableprivate Accessible within the `Student` contract stdRollNumreferred to the student's roll number.Line 8 – 12: We declare an
constructor that acceptsoverloaded Getting parameters as arguments nameandrollNumas arguments that are assigned tostdNameandstdRollNumrespectively.Line 15: We create another smart contract
StudentsListwhich has a list of the typeStudentas a state variable.Line 17: We declare an array of type
Studentreferred tostudentsas a private state variable of contractStudentsList.Line 19 – 25: We define the default constructor with the
constructorkeyword, to create an instance of the smart contractStudent. At the time of instantiation:An instance of
Studentwill be created, referred asnewStudent.Student smart contract will be deployed on the blockchain.
State variables
stdNameandstdRollNumwill be initialized.The constructor of
Studentwill be executed.The address of the new smart contract
Studentwill be returned to theStudentListcontract.
Line 21: We initialize
newStudentby passing parametersBurakand20respectively.Line 24: We add
newStudenttostudentsthe array.
Sending ether gas
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