LibraryState Variables and Visibility

State Variables and Visibility

Learn about State Variables and Visibility as part of Web3 and Decentralized Application Development

State Variables and Visibility in Ethereum Smart Contracts

In Ethereum smart contract development, state variables are fundamental. They represent the data that persists on the blockchain, forming the contract's memory. Understanding how to declare and manage these variables, along with their visibility, is crucial for building secure and functional decentralized applications (dApps).

What are State Variables?

State variables are declared directly in the contract body, outside of any function. Their values are stored permanently on the blockchain. Think of them as the contract's persistent memory, holding information like ownership, balances, or configuration settings.

Where are state variables declared in a Solidity contract?

State variables are declared directly in the contract body, outside of any function.

Visibility Specifiers

Visibility specifiers control which parts of your contract (or external contracts) can access a state variable or function. Solidity provides four visibility specifiers:

code
public
,
code
private
,
code
internal
, and
code
external
. Understanding these is key to managing data access and security.

SpecifierAccessible FromInheritance
publicAll contracts, external accounts, and within the contract itself.Yes
privateOnly within the contract where it is defined. Not accessible by derived contracts.No
internalWithin the contract where it is defined and by derived contracts. Not accessible by external contracts.Yes
externalOnly from external contracts and external accounts. Cannot be called internally by the contract itself.Yes (for functions, not state variables)

For state variables, public means it's accessible externally and internally. internal means it's accessible by derived contracts and internally. private means it's only accessible within the defining contract. external is not a valid visibility specifier for state variables.

Default Visibility

If no visibility specifier is explicitly provided for a state variable, its default visibility is

code
internal
. This is a crucial point for security and predictable behavior.

What is the default visibility for state variables in Solidity if not specified?

The default visibility for state variables is internal.

Practical Examples

Let's consider a simple contract to illustrate. A

code
owner
variable is often
code
public
so anyone can check who owns the contract. A
code
balance
might be
code
internal
or
code
private
if it's only managed by internal functions. A
code
secretKey
should definitely be
code
private
.

Consider a Token contract. It might have a totalSupply variable, which is often public so anyone can query the total supply. It might also have a mapping of balances from addresses to uints. This balances mapping could be internal if only specific functions like transfer or mint modify it, or public if you want to allow direct querying of any address's balance. The owner of the contract is typically a public state variable.

📚

Text-based content

Library pages focus on text content

Best Practices for State Variables and Visibility

Always explicitly declare the visibility of your state variables. This improves code readability and prevents unexpected behavior. Use

code
private
or
code
internal
whenever possible to enforce encapsulation and reduce the attack surface. Only expose state variables as
code
public
if external access is genuinely required.

Principle of Least Privilege: Grant only the necessary permissions. For state variables, this means using the most restrictive visibility specifier that still allows the contract to function as intended.

Learning Resources

Solidity Documentation: State Variables(documentation)

The official Solidity documentation provides a comprehensive overview of state variables, their declaration, and their role in contract storage.

Solidity Documentation: Visibility and Access Modifiers(documentation)

This section of the Solidity docs details the different visibility specifiers (`public`, `private`, `internal`, `external`) and how they apply to functions and state variables.

CryptoZombies: State Variables and Functions(tutorial)

An interactive tutorial that explains state variables and how to use them in a practical smart contract context, building a zombie-fighting game.

Ethereum Development with Solidity: State Variables(documentation)

Ethereum.org's developer documentation offers a clear explanation of smart contract anatomy, including the purpose and usage of state variables.

Solidity Tutorial: Visibility Specifiers(tutorial)

A straightforward tutorial explaining each visibility specifier with code examples, focusing on their impact on accessibility.

Understanding Solidity State Variables and Visibility(blog)

A blog post that delves into the nuances of state variables and visibility, offering practical advice and common pitfalls to avoid.

Solidity by Example: Visibility(documentation)

A collection of concise, runnable Solidity code examples demonstrating various concepts, including different visibility specifiers.

Smart Contract Security: State Variable Management(blog)

While broader, this article touches upon security best practices, including how proper state variable management and visibility contribute to contract security.

Solidity Tutorial: Contract Structure and State(video)

A video course module that covers the fundamental structure of Solidity contracts, with a focus on state variables and their lifecycle.

Solidity State Variables and Visibility Explained(video)

A clear and concise video explanation of Solidity state variables and the different visibility specifiers, with practical examples.