RTL Studio Open IDE

2.5 · SYSTEMVERILOG CLASS

local and static members

Use static and local members to share state and hide internal class data.

Watch on YouTube

What you practice

local and static members (2.5). Use static and local members to share state and hide internal class data. It is in Classes & OOP Basics — Part 2 · Verification & OOP of the SystemVerilog Masterclass. Software-side verification: dynamic data structures, classes, and the first UVM master keys.

The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.

What you will learn

Today you will learn how to use the static keyword to share a variable across all class instances and the local keyword to restrict access to instance-specific data, enabling controlled state management in verification environments.

Learning objectives

  • Understand the difference between static and local class members.
  • Use static to maintain a global counter across object instantiations.
  • Use local to hide internal instance identifiers from external access.
  • Observe how static variables retain value across object construction.

Theory

In SystemVerilog, class members can be declared static or local to control scope and lifetime.

KeywordScopeLifetimePurpose
staticShared across all instancesPersists for entire simulationGlobal counters, shared configuration
localOnly within the class (including methods)Instance-specificHidden internal state, encapsulation

A static variable is allocated once per class, not per object, so all instances reference the same memory location. A local variable cannot be accessed outside the class—even by other instances—enforcing encapsulation.

Practice

  1. Open RtlStudio and select this chapter; confirm student/rtl/design.sv and student/tb/tb.sv appear in Explore.
  2. Open student/tb/tb.sv in the editor.
  3. Locate the class Item definition and replace __TODO__ with static to declare the shared counter.
  4. Run Simulation and check the Log panel for expected output.
  5. (Optional) Open the Waveform tab to inspect DUT signals during stimulus.

Summary

You practiced using the static keyword to maintain a shared counter across all class instances, demonstrating global state behavior. You also used the local keyword to restrict variable visibility within the class, reinforcing encapsulation. This exercise highlighted how static members enable data sharing without requiring instance access, while local members protect internal state from unintended modification. The key takeaway is that static variables persist across object lifetimes and are shared among all instances, whereas local variables remain private to each object's scope.

Run it in the Verilog IDE

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.

The lesson is done when the simulation matches local and static members. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.