6

If I have a public state variable, does this mean that other contracts can access or even change its value?

I have made a cryptocoin from here and all variables are declared as public, so I am concerned if other contracts can only view these variables or can they change the values too?

5

A public state variable will have an automatically generated accessor that other contracts can use to read the variable.

However, public state variables can only be changed by a function in a contract that changes the variable. If there are no such functions that are accessible to other contracts, the state variable cannot be changed by other contracts.

Functions can be specified as being external, public, internal or private, where the default is public.

external: External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works). External functions are sometimes more efficient when they receive large arrays of data.

public: Public functions are part of the contract interface and can be either called internally or via messages. For public state variables, an automatic accessor function (see below) is generated.

internal: Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using this.

private: Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

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.