I just started learning solidity recently. I don't understand why and when should I declare type as contract name. Sometimes we use contract type for array, and sometimes we use it as address type. I don't get it how to use it.
I just started learning solidity recently. I don't understand why and when should I declare type as contract name. Sometimes we use contract type for array, and sometimes we use it as address type. I don't get it how to use it.
It depends on whether you prefix the contract type with the new keyword or not.
With the new keyword, you're passing constructor params (in the example below two strings) and deploying new contract.
Without the new keyword, you're instantiating a pointer to an already deployed contract (in the example below on the address 0x123) and can invoke its public and external functions.
pragma solidity ^0.8;
contract Hello {
constructor(string memory text1, string memory text2) {}
}
contract World {
function foo() external {
// deploying new contract
new Hello("constructor", "params");
// pointing to an already deployed contract
Hello(address(0x123));
}
}