5

Looking at the code of OpenZeppelin's Ownable, I see this public field:

contract Ownable {
  address public owner;
  :
}

Can owner be changed from web3 or other contracts? I would guess no, since it would be a security hole.

Does it means that public fields can be accessed only by the contract itself? (something like a public getter and an internal setter)

Can I define something like:

contract MyContract {
  Person[3] public persons;
  :
  struct Person {...}
}

And be sure that the array can be read and cannot be written by any client?

4

Yes, marking a variable public is equivalent to creating a getter with the same name. There is no built-in support for setters, because you nearly always need some kind of custom code to manage who or how the variable should be set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.