Concept of “This” in System Verilog:

In short: The this keyword is a built-in handle that refers to the current object, the specific instance whose method is running. Its most common use is inside a constructor or method when a parameter has the same name as a class field: this.x = x; means “set this object’s field x to the parameter x”. You only need this to remove that ambiguity, but it always works as a clear way to say “my own member”.

When a method runs, it runs on behalf of one particular object. this is how the method names that object. It is passed to every non-static method automatically, so you never declare it. You reach for it most often when a local variable or argument shadows a class field and you need to tell the two apart, but it is also handy for passing the current object to another method or storing a reference to yourself.

A simple way to picture it

Imagine a room full of identical vending machines, all running the same instruction sheet. When one machine follows the step “add the coin to my own cash box”, the word “my” has to mean that one machine, not the one next to it. this is the word “my”. Every machine runs the same code, but this always points at the machine that is currently acting, so its money goes into its own box.

The classic use: name clashes in a constructor

A constructor often takes an argument with the same name as the field it sets. Without this, the name refers to the closest one, the argument, and the field never gets set. With this, you separate them clearly.

class pixel;
  int x;
  int y;

  function new(int x, int y);
    this.x = x;   // this.x is the field, x is the argument
    this.y = y;
  endfunction

  function void show();
    $display("pixel at (%0d, %0d)", x, y);
  endfunction
endclass

module tb;
  initial begin
    pixel p = new(3, 7);
    p.show();          // pixel at (3, 7)
  end
endmodule

Here this.x is the object’s field and plain x is the constructor argument. Drop the this. and the line x = x; would just assign the argument to itself, leaving the field at its default of 0.

When you do not need this

If there is no name clash, this is optional. Both versions below do the same thing, because count can only mean the field when no local variable shares the name.

class counter;
  int count;

  function void bump();
    count = count + 1;        // fine: no local named count
    // this.count = this.count + 1;  // identical, just more explicit
  endfunction
endclass

Some teams still write this everywhere for clarity. That is a style choice; the compiler treats both the same when there is no shadowing.

Passing the current object with this

this is not only for field access. You can hand the current object to another method, which is exactly what UVM does when it registers callbacks or raises objections.

class node;
  string  name;
  node    next;

  function new(string name);
    this.name = name;
  endfunction

  // Link another node after this one, passing myself along
  function void append(node n);
    this.next = n;
    $display("%s now points to %s", this.name, n.name);
  endfunction
endclass

module tb;
  initial begin
    node a = new("A");
    node b = new("B");
    a.append(b);          // A now points to B
  end
endmodule

In UVM you see this pattern constantly, for example phase.raise_objection(this) tells the phase which component is raising the objection: the one whose method is running.

this vs super

These two keywords are easy to confuse. this points at the current object; super reaches the parent class version of a member. They are not opposites, but they answer different questions.

KeywordRefers toTypical use
thisThe current object instanceSeparate a field from a same-named argument; pass yourself to a method
superThe parent class part of this objectCall the base version, such as super.new() or super.build_phase()

Expected output, in plain words

The pixel example prints “pixel at (3, 7)” because this.x and this.y correctly store the constructor arguments into the object’s fields. The node example prints “A now points to B”. If you removed the this. from the pixel constructor, the show call would print “pixel at (0, 0)” instead, since the fields would never be written. I review this against the language rules rather than run it, so confirm the exact prints in your own simulator or on EDA Playground.

Common mistakes to avoid

  • Forgetting this when names clash. Writing x = x; in a constructor assigns the argument to itself and leaves the field unset. Use this.x = x;.
  • Using this in a static method. Static methods have no current object, so this is not available there.
  • Confusing this with super. this is the current object; super is the parent class version of a member.
  • Thinking this is required everywhere. It is only needed to resolve a name clash; otherwise it is optional and just adds clarity.
  • Expecting this to point at the handle type. It points at the actual object, which matters when a base handle holds a derived object.

Keep learning

The this keyword is part of the class basics. See encapsulation in SystemVerilog for controlling access to the fields you set with this, and inheritance in SystemVerilog for how this and super work together. To see this in a testbench, read the UVM testbench architecture. More is in our SystemVerilog guides and interview questions.

Frequently asked questions

What is the this keyword in SystemVerilog?

this is a built-in handle that refers to the current object, the instance whose method is running. It is passed automatically to every non-static method, so you never declare it. You use it mainly to tell a class field apart from a same-named argument.

When do I need to use this?

You need this when a local variable or method argument has the same name as a class field, so the compiler knows you mean the field. For example this.x = x inside a constructor. When there is no name clash, this is optional.

What is the difference between this and super?

this refers to the current object and its own members. super refers to the parent class version of a member, used for calls like super.new() or super.build_phase(). They answer different questions and are not opposites.

Can I use this in a static method?

No. A static method belongs to the class rather than to any single object, so there is no current object for this to point to. Using this inside a static method is an error.

Does this affect performance?

No. this is resolved at compile time and simply names the current object. Writing it everywhere for clarity has no run-time cost compared with leaving it out where it is not needed.

How is this used in UVM?

UVM methods often take the current component as an argument, such as phase.raise_objection(this) or when registering callbacks. Passing this tells the called method which component or object is acting.

Similar Posts