# Static variable in solidity?

I define a balance as follows :

``````mapping (address => uint[]) Energy_Balance
``````

to keep the history of each transaction. I set its new value as follows :

``````First Transaction: balances[owner][0] = balances[owner][0] + value;
Second Transaction: balances[owner][1] = balances[owner][1] + value;
``````

Now, I want to define a `uint` `counter` such that it can keep its last value every time it is increased one unit as follows :

``````balances[owner][counter] = balances[owner][counter] + value
``````

If I define counter as follows, is it correct in solidity and it can keep its last value ?

``````static uint counter = 0;
function myFunction(uint value) {
balances[owner][counter] = balances[owner][counter] + value;
counter = counter +1;
}
``````

You can simplify.

``````function myFunction(uint value) {
balances[owner].push(value);
}
``````

Also, some useful getters:

``````function getOwnerValueCount(address owner) returns(uint count) {
return balances[owner].length;
}
``````

Add `public` to your mapping for `Energy_Balances` and you will get a "free" getter approximately:

``````function Energy_Balances(address owner, uint row) public view returns(uint) {
return Energy_Balances[owner][row];
}
``````

Aside: It would be more conventional to camelCase `energyBalances`.

Hope it helps.

• Thankyou. do you mean in case of using "energyBalances[owner].push(value);" I don't need to define energyBalances as an array like this : mapping (address => uint[]) energyBalances ? Apr 27, 2018 at 13:10
• No. Assuming you did. `mapping (address=>uint [])` means there's an array each address. `push` appends another row, so you don't need to manage the counter yourself. Apr 27, 2018 at 14:08