Within an instance method or a constructor, this is a reference to the current object , the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
For example, in the packet class was written like this
EXAMPLE class packet ; int length = 0;
//constructor function new (int l);
length = l; endfunction endclass
but it could have been written like this:
EXAMPLE: class packet ; int length = 0;
//constructor function new (int length); this.length = length; endfunction endclass
program main;
initial begin
packet pkt;
pkt =new(10);
$display(pkt.length); end
endprogram
RESULT
10
Each argument to the second constructor shadows one of the object's fields inside the constructor "length" is a local copy of the constructor's first argument. To refer to the legnth field inside the object , the constructor must use "this.length".