The this keyword refers to the current instance of a class. It is used primarily in the following situations:
- To differentiate between class properties and local variables or function arguments when they share the same name.
- To explicitly refer to class properties or methods to improve code readability.
- To pass the current class instance as an argument to functions or tasks.
It is particularly useful when there is ambiguity between class properties and function arguments, or when you want to emphasize that a particular variable belongs to the class instance.
Syntax
The this keyword is followed by the name of the property or method:
this.property_name; // Refers to the class instance property
this.method_name(); // Calls a method of the current class instance
Example 1: Resolving Naming Conflicts
When a function or task argument has the same name as a class property, the this keyword resolves ambiguity by explicitly referring to the class property.
class Example;
int value; // Class property
// Constructor with a parameter having the same name as the class property
function new(int value);
this.value = value; // Use 'this' to refer to the class property
endfunction
// Display function
function void display();
$display("Value: %0d", this.value);
endfunction
endclass
module tb;
initial begin
Example ex = new(42); // Pass 42 to the constructor
ex.display(); // Call the display function
end
endmodule
Explanation:
- Naming Conflict: The constructor argument
valuehas the same name as the class propertyvalue. - Using
this:this.valueensures the class property is assigned, not the local parameter. - Output: The value
42is correctly assigned to the class property and displayed.
Output:
Value: 42
Example 2: Calling Methods Explicitly
The this keyword can also be used to explicitly call methods of the current class instance.
class Calculator;
int a, b;
// Constructor
function new(int a, int b);
this.a = a;
this.b = b;
endfunction
// Method to add numbers
function int add();
return this.a + this.b; // Explicitly refer to class properties
endfunction
// Method to display the result
function void display_result();
$display("Sum: %0d", this.add()); // Use 'this' to call a method
endfunction
endclass
module tb;
initial begin
Calculator calc = new(10, 20);
calc.display_result(); // Calls the display function
end
endmodule
Explanation:
this.aandthis.b: Explicitly refer to class propertiesaandb.this.add(): Calls theadd()method of the current instance.- Output: The
addmethod correctly calculates the sum and displays Sum: 30.
Output:
Sum: 30
Example 3: Using this in Inheritance
When using inheritance, the this keyword refers to the current instance of the class, including inherited properties and methods.
class Base;
int base_value;
function void show();
$display("Base Value: %0d", this.base_value);
endfunction
endclass
class Derived extends Base;
int derived_value;
function void show();
$display("Derived Value: %0d", this.derived_value);
super.show(); // Call base class method
endfunction
endclass
module tb;
initial begin
Derived d = new();
d.base_value = 10;
d.derived_value = 20;
d.show(); // Calls show method in derived class
end
endmodule
Explanation:
this.base_valueandthis.derived_value: Refer to the current instance properties.super.show(): Calls the method in the base class.- Output: Displays both the base and derived class values.
Output:
Derived Value: 20
Base Value: 10