The this Keyword in SystemVerilog: When and Why (Tested)
In short: this is a handle to the object whose method is running. Use it when a local name hides a class member, as in this.tav_len = tav_len;, and when an object needs to hand itself to someone else, as in create("drv", this). It always means the whole object, so a virtual call through this runs the child’s version, while super reaches the parent’s.
You type this in almost every UVM component you write, usually as the last argument of create(). Most of the time nobody explains what it is doing there.
In this lesson we test 8 uses of this in one UVM bench, plus the one-word bug that leaves a constructor quietly broken. I ran it on Synopsys VCS.
Start here: the simplest example
Two classes that differ by one word. Both constructors take an argument with the same name as the member.
// tav_this_simple.sv : the smallest "this" example (UVM 1.2 reporting only)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
`include "uvm_macros.svh"
class tav_good;
int tav_len;
function new(int tav_len);
this.tav_len = tav_len; // this.tav_len is the member
endfunction
endclass
class tav_bad;
int tav_len;
function new(int tav_len);
tav_len = tav_len; // both names mean the argument
endfunction
endclass
module tav_top;
import uvm_pkg::*;
tav_good g;
tav_bad b;
initial begin
g = new(4);
b = new(4);
`uvm_info("TAV_SIMPLE", $sformatf("1 with this : g.tav_len = %0d", g.tav_len), UVM_NONE)
`uvm_info("TAV_SIMPLE", $sformatf("2 without this : b.tav_len = %0d", b.tav_len), UVM_NONE)
end
endmodule
Here is what VCS printed:
UVM_INFO testbench.sv(30) @ 0: reporter [TAV_SIMPLE] 1 with this : g.tav_len = 4
UVM_INFO testbench.sv(31) @ 0: reporter [TAV_SIMPLE] 2 without this : b.tav_len = 0
- Line 1:
this.tav_len = tav_len;writes the argument into the member, so the member is 4. - Line 2:
tav_len = tav_len;means the argument on both sides, so the member is never written and stays 0.
The rest of this lesson builds on this. The full bench further down tests the same idea from more angles, with a planted bug.
What you will learn
- What
thispoints to, and where you can use it. - The name clash that makes
thisnecessary in constructors. - How to pass or return the current object.
- Why
thisinside a parent’s method still reaches the child. - The difference between
this,superandlocal::.
Cheat sheet: when to write this
| Use | Code | Why |
|---|---|---|
| Name clash with an argument | this.tav_len = tav_len; | Without this, both names mean the argument |
| Pass yourself to another object | sb.tav_register(this); | The other object gets a handle to you |
| Return yourself | return this; | The caller gets the same object back |
| Parent of a UVM component | create("drv", this) | The env becomes the driver’s parent |
| Parent’s version of a method | super.describe() | Skips your own override |
| Caller’s variable in an inline constraint | with { tav_len == local::tav_len; } | Plain names mean the item, not the caller |
Analogy: this is the word “me”. In a room with two people called Ravi, “Ravi” is unclear, but “me” always means the one speaking.
The name clash
Inside a method, the closest name wins. A constructor argument called tav_len hides the member tav_len, so tav_len = tav_len; copies the argument into itself.
p.tav_len: 4 with this, 0 without it.- Fix 1: write
this.tav_len = tav_len;. This is what UVM code does everywhere. - Fix 2: give the argument another name, such as
len. Thenthisis optional, as fortav_id = id;in check 2.
Where you can and cannot use this
| Place | Allowed? |
|---|---|
| Normal class methods and constructors | Yes |
| Constraints inside the class | Yes |
| Static methods | No: there is no object, so there is no this |
| Modules, interfaces, programs | No: this belongs to classes |
this vs super vs local::
| Means | Typical use | |
|---|---|---|
this | The whole current object | Name clashes, passing or returning yourself |
super | The parent part of the current object | super.new(), reusing a parent method in an override |
local:: | The caller’s scope, inside randomize() with | Comparing an item field with a variable of the same name in the caller |
Going further: one bench, 8 checks
tav_pkthas a constructor argument named like its member, passesthisto a scoreboard, and returnsthisfrom a method.tav_err_pktextends it, to test virtual calls throughthisandthisvssuper.tav_envbuilds a driver withcreate("drv", this).tav_itemis randomized with an inline constraint that useslocal::.- Planted bug:
+BUG=1dropsthisfrom the constructor.
Run it yourself: the same file is on GitHub, with the expected output from both runs.
// tav_this_uvm.sv : the "this" keyword in SystemVerilog, a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// What is in here:
// tav_pkt a class whose constructor argument has the same name as a member
// tav_err_pkt a child of it, to show this vs super and virtual dispatch through this
// tav_sb a tiny scoreboard that objects register themselves with, using this
// tav_item a UVM transaction, for an inline constraint with local::
// tav_env builds a driver with create("drv", this)
// tav_this_test 8 self-checks, each printed with `uvm_info and checked with `uvm_error
//
// Run options:
// (none) all 8 checks pass, 0 errors
// +BUG=1 the tav_pkt constructor writes "tav_len = tav_len;" without this, so check 1 fails
`include "uvm_macros.svh"
package tav_this_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
typedef class tav_pkt;
// ---------------------------------------------------------------
// A tiny scoreboard: objects hand themselves to it with this
// ---------------------------------------------------------------
class tav_sb;
tav_pkt tav_seen[$];
function void tav_register(tav_pkt p);
tav_seen.push_back(p);
endfunction
endclass
// ---------------------------------------------------------------
// Base class
// ---------------------------------------------------------------
class tav_pkt;
int tav_len; // same name as the constructor argument
int tav_id; // different name from the argument
static int tav_bug;
function new(int tav_len, int id, tav_sb sb);
if (tav_bug == 1)
tav_len = tav_len; // BUG: both names mean the argument, the member stays 0
else
this.tav_len = tav_len; // this.tav_len is the member, tav_len is the argument
tav_id = id; // no clash, so no this needed
sb.tav_register(this); // pass the current object to someone else
endfunction
function tav_pkt tav_self();
return this; // a method can hand back its own object
endfunction
virtual function string vkind();
return "base";
endfunction
function string tav_ask();
return this.vkind(); // this is the OBJECT, so a child object answers
endfunction
virtual function string describe();
return "base";
endfunction
endclass
// ---------------------------------------------------------------
// Child class
// ---------------------------------------------------------------
class tav_err_pkt extends tav_pkt;
function new(int tav_len, int id, tav_sb sb);
super.new(tav_len, id, sb);
endfunction
virtual function string vkind();
return "err";
endfunction
virtual function string describe();
return "child";
endfunction
function string tav_both();
return {this.describe(), "/", super.describe()}; // this: my version, super: the parent's
endfunction
endclass
// ---------------------------------------------------------------
// UVM pieces
// ---------------------------------------------------------------
class tav_item extends uvm_sequence_item;
`uvm_object_utils(tav_item)
rand int tav_len;
constraint c_len { tav_len inside {[1:100]}; }
function new(string name = "tav_item");
super.new(name);
endfunction
endclass
class tav_drv extends uvm_component;
`uvm_component_utils(tav_drv)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
endclass
class tav_env extends uvm_env;
`uvm_component_utils(tav_env)
tav_drv drv;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
drv = tav_drv::type_id::create("drv", this); // this = the env, the new driver's parent
endfunction
endclass
// ---------------------------------------------------------------
// The test
// ---------------------------------------------------------------
class tav_this_test extends uvm_test;
`uvm_component_utils(tav_this_test)
tav_env env;
int tav_pass, tav_fail;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
void'($value$plusargs("BUG=%d", tav_pkt::tav_bug));
env = tav_env::type_id::create("env", this);
endfunction
function void tav_check(string what, string got, string exp);
if (got == exp) begin
tav_pass++;
`uvm_info("TAV_THIS", $sformatf("PASS %s: %s", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_THIS", $sformatf("FAIL %s: got %s, expected %s", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
tav_sb sb = new();
tav_pkt p, q, b;
tav_err_pkt e;
tav_item it;
int tav_len = 5; // a local with the same name as the item's field
phase.raise_objection(this);
p = new(4, 7, sb);
// 1. this.tav_len = tav_len: the member gets the argument
tav_check("1 p.tav_len (name clash, uses this)", $sformatf("%0d", p.tav_len), "4");
// 2. No clash: tav_id = id works without this
tav_check("2 p.tav_id (no clash)", $sformatf("%0d", p.tav_id), "7");
// 3. The constructor passed this to the scoreboard: it holds the same object
tav_check("3 sb holds p itself (1 = same handle)", $sformatf("%0d", sb.tav_seen[0] == p), "1");
// 4. A method that returns this gives back the same object
q = p.tav_self();
tav_check("4 p.tav_self() == p", $sformatf("%0d", q == p), "1");
// 5. this inside a base-class method still means the whole object
e = new(8, 9, sb);
b = e;
tav_check("5 b.tav_ask() on a child object", b.tav_ask(), "err");
// 6. this.method() vs super.method() in the child
tav_check("6 e.tav_both() (this / super)", e.tav_both(), "child/base");
// 7. In UVM, create("drv", this) makes the env the parent
tav_check("7 drv parent / full name",
$sformatf("%s / %s", env.drv.get_parent().get_full_name(), env.drv.get_full_name()),
"uvm_test_top.env / uvm_test_top.env.drv");
// 8. Inline constraint: plain names mean the item, local:: means the caller
it = tav_item::type_id::create("it");
if (!it.randomize() with { tav_len == local::tav_len; })
`uvm_error("TAV_THIS", "randomize() failed")
tav_check("8 it.tav_len with local::tav_len", $sformatf("%0d", it.tav_len), "5");
`uvm_info("TAV_THIS", $sformatf("Checks: %0d pass, %0d fail", tav_pass, tav_fail), UVM_NONE)
phase.drop_objection(this);
endtask
endclass
endpackage
module tav_top;
import uvm_pkg::*;
import tav_this_pkg::*;
initial run_test("tav_this_test");
endmodule
How to run it
- Open EDA Playground and pick Synopsys VCS with UVM 1.2.
- Paste the file into
testbench.svand leavedesign.svempty. - Run it once as is, then add
+BUG=1to the run options and run again.
What VCS returned
| # | Check | Result | What it proves |
|---|---|---|---|
| 1 | this.tav_len = tav_len | 4 | this reaches the hidden member |
| 2 | tav_id = id | 7 | No clash, no this needed |
| 3 | Scoreboard holds the object passed as this | 1 | It is the same handle, not a copy |
| 4 | p.tav_self() == p | 1 | return this gives back the same object |
| 5 | Base method calls this.vkind() on a child object | err | this is the object, so the child answers |
| 6 | Child’s this.describe() / super.describe() | child/base | super skips your override |
| 7 | create("drv", this) parent / full name | uvm_test_top.env / uvm_test_top.env.drv | this made the env the parent |
| 8 | with { tav_len == local::tav_len; } | 5 | local:: reached the caller’s variable |
Result: 8 pass, 0 fail. Here is the log from the clean run.
UVM_INFO @ 0: reporter [RNTST] Running test tav_this_test...
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 1 p.tav_len (name clash, uses this): 4
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 2 p.tav_id (no clash): 7
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 3 sb holds p itself (1 = same handle): 1
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 4 p.tav_self() == p: 1
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 5 b.tav_ask() on a child object: err
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 6 e.tav_both() (this / super): child/base
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 7 drv parent / full name: uvm_test_top.env / uvm_test_top.env.drv
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 8 it.tav_len with local::tav_len: 5
UVM_INFO testbench.sv(195) @ 0: uvm_test_top [TAV_THIS] Checks: 8 pass, 0 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 12
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_THIS] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
The planted bug: one missing word
With +BUG=1, the constructor says tav_len = tav_len;. It compiles, it runs, and nothing in the run log flags the line. Only check 1 fails, with got 0, expected 4.
UVM_INFO @ 0: reporter [RNTST] Running test tav_this_test...
UVM_ERROR testbench.sv(149) @ 0: uvm_test_top [TAV_THIS] FAIL 1 p.tav_len (name clash, uses this): got 0, expected 4
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 2 p.tav_id (no clash): 7
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 3 sb holds p itself (1 = same handle): 1
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 4 p.tav_self() == p: 1
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 5 b.tav_ask() on a child object: err
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 6 e.tav_both() (this / super): child/base
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 7 drv parent / full name: uvm_test_top.env / uvm_test_top.env.drv
UVM_INFO testbench.sv(145) @ 0: uvm_test_top [TAV_THIS] PASS 8 it.tav_len with local::tav_len: 5
UVM_INFO testbench.sv(195) @ 0: uvm_test_top [TAV_THIS] Checks: 7 pass, 1 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 11
UVM_WARNING : 0
UVM_ERROR : 1
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_THIS] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
The lesson: a self-assignment is legal code, so the simulator will not save you. Use this. on every member that shares a name with an argument, or avoid the shared names.
Check 8 in detail: this in the caller, local:: for the caller
In it.randomize() with { ... }, plain names are looked up in the item first. So tav_len == tav_len compares the item’s field with itself, which is always true and constrains nothing.
Writing local::tav_len tells the solver to use the caller’s tav_len, which is 5 in the bench. VCS returned 5.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
tav_len = tav_len; in a constructor | The member keeps its default (+BUG=1) | this.tav_len = tav_len; |
this in a static method | Compile error | Make the method non-static, or pass the object in |
create("drv", null) or a missing parent | The driver hangs off the top, not the env | Pass this from the parent’s build_phase |
Same name in caller and item, no local:: | The inline constraint does nothing | local::name or a different name |
Expecting this in a parent method to mean the parent class | The child’s override runs (check 5) | Use super, or make the method non-virtual |
How AI helps you master this
- Find self-assignments: paste your classes and ask for every line that assigns a name to itself.
- Explain a name lookup: paste a
randomize() withblock and ask which names belong to the item and which to the caller. - Check parents: paste a
build_phaseand ask which components get the wrong parent.
Warning: a convincing AI answer is not the same as a correct engineering result. Ask an AI tool whether tav_len = tav_len; sets the member and it may say yes, so run the code.
Interview questions on this
- What does
thisrefer to? The object whose method is running. - Why is
thisneeded inthis.x = x;? The argumentxhides the member, sothisis needed to reach the member. - Can you use
thisin a static method? No. A static method has no object. - What is the difference between
thisandsuper?thisis the whole object.superreaches the parent’s version of a member or method. - Why do UVM components pass
thistocreate()? It sets the new component’s parent, which builds the hierarchy and its full path name.
More practice: SystemVerilog interview questions.
Practice challenge
- Run the bench and confirm 8 pass, 0 fail.
- Run with
+BUG=1and explain why the member is 0. - Remove
local::from check 8. Predict whatit.tav_lencan be, then run it a few times with different seeds. - Bonus: make
vkind()non-virtual intav_pkt. What does check 5 return now, and why?
FAQ
What is the this keyword in SystemVerilog? A built-in handle to the current object, usable inside non-static class methods and constraints.
When do I need to use this? When a local name, usually an argument, hides a member, and when you pass or return the current object.
What is the difference between this and super? this is the whole object, so virtual calls through it run the child’s version. super calls the parent’s version.
Can I use this in a static method? No. Static methods belong to the class, not to an object.
Does this affect performance? No. this.x and x reach the same member when there is no clash.
How is this used in UVM? Mostly as the parent argument, as in create("drv", this), and in raise_objection(this) to say which component raised it.
References
- IEEE 1800-2017, SystemVerilog standard: section 8.11 (this), section 8.15 (super), and section 18.7.1 (local:: in inline constraints).
Keep learning
- The OOP set: inheritance, polymorphism and virtual vs pure virtual methods.
- Where this meets the factory: create() vs new() in UVM.
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification
