[Ethereum] Automatically create setters and getters


I've read that getters are automatically created when you make a variable public. Can other contracts access these getters or is it only for web3.eth? Also what would be the getter name?

Best Answer

The getter's name is the variable's name. So in

contract foo {
    int public bar;

You can access it through someFooInstance.bar(). It should work the same in both web3.js and solidity (from another contract.) If it's an array (i.e. int[] public bar) or a mapping (mapping (address=>int) public bar) you can access specific elements through someFooInstance.bar(n).

There's no way to automatically create a setter, because a setter anyone can access is probably a bad idea. But there's no obvious way to decide who can and can't access a setter.

Related Topic