Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Class Handle: What Is a Class Handle?

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog Class Handle tutorial (What Is a Class Handle?)

Open in IDE
testbench.sv
// Create a new class with a single member called
// count that stores integer values
class Packet;
	int count;
endclass

module tb;
  	// Create a "handle" for the class Packet that can point to an
  	// object of the class type Packet
  	// Note: This "handle" now points to NULL
	Packet pkt;
  
  	initial begin
      if (pkt == null)
        $display ("Packet handle 'pkt' is null");
  
      // Call the new() function of this class
      pkt = new();
  
      if (pkt == null)
        $display ("What's wrong, pkt is still null ?");
      else
        $display ("Packet handle 'pkt' is now pointing to an object, and not NULL");
  
      // Display the class member using the "handle"
      $display ("count = %0d", pkt.count);
  	end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.