Solidity 1-Variables and Constructors in Solidity
The file name of the smart contracts in Solidity is written as 'name.sol'. For example, MyContract.sol
pragma solidity >=0.8.2 <0.9.0; // This is the 'pragma directive' defined at the beginning of a solidity smart contract. The statement tells the compiler about which version of Solidity is used to write the contract i.e., greater or equal to 0.8 point two and less than 0.9 point 0.
contract MyContract {
}
The name "MyContract" should be preceded by the keyword 'contract' followed by a set of curly braces. The set of braces encloses the body of the contract within the boy of the contract.
We define state variables, functions and events to specify the behavior of the contract (similar to the class in Java or other OOPs).
'Variables' are used to store data values. Solidity supports different types of variables. 'State variables' are declared inside the contract but outside the functions. The state variables are stored permanently on the contract storage of Ethereum Blockchain. Thus, we have to pay 'gas fees' for we are using the contract storage. For this very reason, we should be careful while using the numbers of state variables in the contracts because more gas fee will be applicable.
contract MyContract {
uint public testNumber = 100;
}
In the above statement, 'uint' is the datatype, 'public'/or 'private' is the 'visibility' and 'testNumber' is the variable name. 'Uint' stands for unsigned integer (non negative integer) that does not include a sign bit. This means 'uint' cannot represent negative numbers. However, using 'int' means the variable can store both 'positive' and 'negative' integer values. The visibility 'public' in the above case means the state variable is available for any external call. The 'getter' function is implicitly created by the compiler to read its value. On the contrary, we have the 'internal' visibility, meaning state variables are not accessible from any external calls i.e., externally via transactions. They can be accessed only from within the contract or from the contract that are deriving it. If the visibility is not specified, the visibility is treated as 'private' that is, state variables are neither accessible from any external calls nor from any other child contracts that are deriving it. They are accessible from the functions inside the contract.
The following diagram shows the output after deploying and running the contract.
Deploy and run the transaction
If success, we will have 'green check mark'. Click under 'Deployed Contracts' to expand and see Solidity compiler creating a function for the public state variable. Click on the variable 'testNumber', and you will get to see the value '100'. This is the way to initialize the state variables during the declaration.
pragma solidity >=0.8.2 <0.9.0;
contract MyContract {
uint public testNumber;
constructor() {
testNumber = 200;
}
}
The other way to initialize the state variable is through 'constructor' (without parameters) as shown above. 'Constructor' is a special function that is executed once when the contract is first deployed to the Blockchain.
pragma solidity >=0.8.2 <0.9.0;
contract MyContract {
uint public testNumber;
constructor(uint new_num){
testNumber = new_num;
}
}
The other way to initialize the state variable is through 'constructor' (with parameters) as shown above.As constructor is a function that will be called during the deployment of the smart contract, we need to pass an argument to this constructor. In our case, new_num gets assigned to testNumber that we have declared as the state variable. Let us deploy and assign the value '10' to the variable.
Note:
To comment out the statements of the program, highlight the lines to be commented out and press 'control slash key' on your keyboard. You will get to see the following as I highlighted line numbers 6 to 10 only.
pragma solidity >=0.8.2 <0.9.0;
contract MyContract {
// uint public testNumber;
// constructor(uint new_num){
// testNumber = new_num;
// }
}
Let's talk bout the local variables. These variables are declared inside a function (scope limited to the function itself and are temporary in nature). In addition, they do not require gas fee.
pragma solidity >=0.8.2 <0.9.0;
contract MyContract {
// uint public testNumber;
// constructor(uint new_num){
// testNumber = new_num;
// }
function show() public pure returns(uint){
uint test = 10;
return test;
}
}
Deploying this contract will give us the following
Will review your comment and get back!