![]() |
VOOZH | about |
Solidity Contracts are like a class in any other object-oriented programming language. They firmly contain data as state variables and functions which can modify these variables. When a function is called on a different instance (contract), the EVM function call happens and the context is switched in such a way that the state variables are inaccessible. A contract or its function needs to be called for anything to happen. Some basic properties of contracts are as follows :
Creating contracts programmatically is generally done by using JavaScript API web3.js, which has a built-in function web3.eth.Contract to create the contracts. When a contract is created its constructor is executed, a constructor is an optional special method defined by using the constructor keyword which executes one per contract. Once the constructor is called the final code of the contract is added to the blockchain.
Syntax:
contract <contract_name>{
constructor() <visibility>{
.......
}
// rest code
}
Example: In the below example, the contract Test is created to demonstrate how to create a contract in Solidity.
Output :
Solidity provides four types of visibilities for functions and state variables. Functions have to specified by any of the four visibilities but for state variables external is not allowed.
Example: In the below example, the contract contract_example is created to demonstrate different visibility modifiers discussed above.
Output :
👁 Visibility Modifiers Example