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 brackets
e.g: Contract_Name variableName = new Contract_Name(arguments);
else we will leave brackets empty
e.g: Contract_Name variableName = new Contract_Name();
*/

Result

The initialization of a smart contract using the new keyword results in:

  1. Creation of instance of a new contract.

  2. A contract having (composed of) the instance of other contract as state variableDeployment of the new smart contract on the blockchain.

  3. Initialization of the state variables.

  4. Execution of the constructor of the new smart contract.

  5. Assignment of nonce`number used once` is increased by 1 after every transaction value to one.

  6. 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 variables
constructor (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` keyword
Student newStudent = new Student("Burak", 20);
// pushing into students array
students.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 smart contractA smart contract is a collection of state variables and functions to manipulate the state variables. named Student.

  • Line 4: We declare a private state variableVariables whose values are permanently stored in a contract storage. stdName, referred to the student's name.

  • Line 5: We declare another privateAccessible within the `Student` contract state variable stdRollNum referred to the student's roll number.

  • Line 8 – 12: We declare an overloadedGetting parameters as arguments constructor that accepts 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.

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

Copyright ©2025 Educative, Inc. All rights reserved