SystemVerilog OOP Mastery · All levels

Classes and Objects

A class declares a type and behavior, but no storage exists until you call new(). This separation is central in verification because transaction objects are created dynamically and passed through drivers, monitors, and scoreboards by handle.

Overview

A class declares a type and behavior, but no storage exists until you call new(). This separation is central in verification because transaction objects are created dynamically and passed through drivers, monitors, and scoreboards by handle.

A class is a blueprint; the simulator allocates an actual object only when new() executes. Declaring packet p; creates a handle variable that can point to an object, and its initial value is null. In practice this means object lifetime is explicit and tied to construction points in your testbench flow.

In this topic

diagram
1. Concept Explained
2. Code Examples
3. SystemVerilog vs C++/Java
4. Pitfalls
5. Exercises
6. Interview Questions

Read the concept first, study the code examples, compare against C++/Java, then test yourself with the exercises and interview questions.

Class vs handle vs object memory model

diagram
CLASS VS HANDLE VS OBJECT IN MEMORY

compile-time blueprint                    runtime state
+---------------------------+             +-------------------------+
| class packet;             |             | stack frame             |
|   rand bit [7:0] addr;    |             |   packet p ------------+----+
|   function void display() |             |                         |    |
| endclass                  |             +-------------------------+    |
+---------------------------+                                          |
                                                                       v
                                                           +-------------------------+
                                                           | heap object @0x1004     |
                                                           | addr = 8'h3C            |
                                                           | display() body executes |
                                                           +-------------------------+

Class defines behavior. Handle stores a reference. Object lives on heap.

Related topics