SystemVerilog OOP Mastery · All levels
Static Members and Static Methods in Class-Based Testbenches: Code Examples
Code Examples for Static Members and Static Methods in Class-Based Testbenches.
Code examples
Shared ID allocator
systemverilog
class txn #(type T = int);
static int next_id = 0;
int id;
T payload;
function new(T payload = '0);
this.payload = payload;
id = next_id++;
endfunction
static function void reset_ids();
next_id = 0;
endfunction
endclassEvery txn#(T) object shares one counter per specialization, and reset_ids() gives deterministic startup behavior across tests.
Static method using explicit object handle
systemverilog
class pkt;
static int total = 0;
int len;
function new(int len);
this.len = len;
total++;
endfunction
static function bit has_valid_len(pkt p);
return (p != null) && (p.len > 0);
endfunction
endclassThe static method cannot read len directly from class scope, so it accepts pkt p and reads instance data through that handle.