What is the use of the new
keyword for creating new smart contracts? Why not just omit this keyword?
There are two ways you can create contracts
Using new keyword you instantiate the new instance of the contract and use that newly created contract instance
While in latter option you use the address of the already deployed and instantiated contract. You can check below code for reference:
pragma solidity ^0.5.0;
contract Communication {
string public user_message;
function getMessage() public view returns (string memory) {
return user_message;
}
function setMessage(string memory _message) public {
user_message = _message;
}
}
contract GreetingsUsingNew {
function sayHelloUsingNew() public returns (string memory) {
Communication newObj = new Communication();
newObj.setMessage("Contract created using New!!!");
return newObj.getMessage();
}
}
contract GreetingsUsingAddress {
function sayHelloUsingAddress(address _addr) public returns (string memory) {
Communication addObj = Communication(_addr);
addObj.setMessage("Contract created using an Address!!!");
return addObj.getMessage();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With