SourceSV Verification Directory

This Keyword

In SystemVerilog, this keyword is used within a class to refer to the current instance of that class. It is a way to access the members (attributes and methods) of the current object from within the object’s methods. This is particularly useful to distinguish between class attributes and parameters or local variables with the same name. When a variable is declared, SystemVerilog looks in the current scope for it and then in the parent scope until the variable is found. The this keyword is a way of keeping the search within the class type, skipping variables declared within the method as well as anything outside the class type hierarchy. It should only be used in non-static class methods.

class Ex_class;
   // class properties or variables
  int a, b, s;

   // places the arguments inside the class properties
  function new(int a, b);
    this.a = a;
    this.b = b;
  endfunction

   // task method
  task sum;
    s = a + b;
    $display("a = %0d , b = %0d, sum s = %0d", this.a, this.b ,s);
  endtask

endclass


module tb_this_keyword;

  Ex_class cl_obj;          // declare a class handle of type cl_obj

   initial begin
     cl_obj = new(30,10);   // construct an object
     cl_obj.sum;            // access the class method
   end

endmodule

Execute the code in EDA Playground