Consider the following code:
class my_class;
// *** constructor
function new(string name = "");
super.new(name);
endfunction
// ** my_function
function void my_function();
static int my_static_int = 0;
my_static_int++;
$display("my_static_int = %0d", my_static_int);
endfunction
endclass
In my code, Assume I have a sequence with body -
// ** body
task body();
my_class my_class_A = new;
my_class_A.my_function();
endtask
My issue is that if I were to call body
twice from my test, the second time, my_static_int
has the value 2 instead of what I expected which is 1.
Since my_class_A
is destroyed each time body is run (I verify this using %p), shouldn't the subsequent run on a completely different object of my_class
print a value of 1 again
Maybe my understanding is lacking here.. Any help is appreciated
A
static
variable means there is only one copy of the variable and one initialization of that variable before time 0. Without thestatic
keyword, it would have been an automatic variable that gets allocated and initialized each time your function gets called.If you want a variable that gets initialized each time the object gets constructed, simply declare it as a class member variable.