Quiz Entry - updated: 2026.07.30
When you mark a state variable public, what does Solidity generate for you automatically?
A getter function of the same name that returns the variable's value — which is why a public variable shows up as a callable button.
uint256 public favoriteNumber;
// Solidity auto-creates: function favoriteNumber() view returns (uint256)
Declaring a variable public is shorthand for "also give me a read-only accessor." Solidity synthesises a view function named after the variable that simply returns its current value. So public variables are, secretly, function calls. For a public array or mapping, the generated getter takes the index/key as a parameter and returns the element at that position. This is the reason a public variable costs nothing to read externally — its auto-getter is a view function.
Go deeper:
Solidity docs: getter functions — what the auto-generated accessor looks like, including for arrays and mappings.