Static Property
A static property is a class variable that is associated with the class, rather than with an instance of the class (a.k.a., an object). This means that when it is changed, its change is reflected in all instances of the class. Static properties are declared with the static keyword.
class A;
static int unsigned i;
endclass
module top;
A obj_1;
A obj_2;
A obj_3;
A obj_4;
initial begin
obj_1 = new();
obj_2 = new();
$display("1. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
obj_1.i = 123; //i is change in all instance of class A.
$display("2. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
obj_2.i = 132; //i is change in all instance of class A.
$display("3. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
//The static class properties can be accessed using class name.
obj_1.i = 321;
$display("1. A::i = %0d", A::i);
obj_2.i = 213;
$display("2. A::i = %0d", A::i);
//static class properties can be accessed without creating object
obj_3.i = 345;
$display("1. obj_3.i = %0d", obj_3.i);
//static class properties can be accessed using the object name, without creating object
obj_4.i = 453;
$display("2. A::i = %0d", A::i);
end
endmodule : top
//Output:
// 1. obj_1.i = 0, obj_2.i = 0
// 2. obj_1.i = 123, obj_2.i = 123
// 3. obj_1.i = 132, obj_2.i = 132
// 1. A::i = 321
// 2. A::i = 213
// 1. obj_3.i = 345
// 2. A::i = 453
static int unsigned i;
endclass
module top;
A obj_1;
A obj_2;
A obj_3;
A obj_4;
initial begin
obj_1 = new();
obj_2 = new();
$display("1. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
obj_1.i = 123; //i is change in all instance of class A.
$display("2. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
obj_2.i = 132; //i is change in all instance of class A.
$display("3. obj_1.i = %0d, obj_2.i = %0d", obj_1.i, obj_2.i);
//The static class properties can be accessed using class name.
obj_1.i = 321;
$display("1. A::i = %0d", A::i);
obj_2.i = 213;
$display("2. A::i = %0d", A::i);
//static class properties can be accessed without creating object
obj_3.i = 345;
$display("1. obj_3.i = %0d", obj_3.i);
//static class properties can be accessed using the object name, without creating object
obj_4.i = 453;
$display("2. A::i = %0d", A::i);
end
endmodule : top
//Output:
// 1. obj_1.i = 0, obj_2.i = 0
// 2. obj_1.i = 123, obj_2.i = 123
// 3. obj_1.i = 132, obj_2.i = 132
// 1. A::i = 321
// 2. A::i = 213
// 1. obj_3.i = 345
// 2. A::i = 453
Static Method
Methods can be declared as static. A static method is subject to all the class scoping and access rules, but behaves like a regular subroutine that can be called outside the class, even with no class instantiation.
class A;
static task incr();
int unsigned j; //automatic variable
j++;
$display("J is %d",j);
endtask
endclass
module top;
A obj_1;
A obj_2;
initial begin
$display("Static task - static task with automatic variables");
obj_1 = new();
obj_2 = new();
obj_1.incr();
obj_2.incr();
obj_1.incr();
obj_2.incr();
obj_1.incr();
$display("Static task - Each call to task will create a separate copy of 'j' and increment it");
end
endmodule : top
//Output:
// Static task - static task with automatic variables
// J is 1
// J is 1
// J is 1
// J is 1
// J is 1
// Static task - Each call to task will create a separate copy of 'j' and increment it
A static method has no access to nonstatic members (class properties or methods), but it can directly access static class properties or call static methods of the same class. Access to nonstatic members or to the special "this" handle within the body of a static method is illegal and results in a compiler error.
class A;
int unsigned j;
static task incr();
j++;
$display("J is %d",j);
endtask
endclass
module top;
A obj_1;
A obj_2;
initial begin
obj_1 = new();
obj_2 = new();
obj_1.incr();
obj_2.incr();
end
endmodule : top
//Output:
// (Compilation Error) Illegal to access non-static property 'j' from a static method.
class A;
int unsigned j;
static task incr();
j++;
$display("J is %d",j);
endtask
endclass
module top;
A obj_1;
A obj_2;
initial begin
obj_1 = new();
obj_2 = new();
obj_1.incr();
obj_2.incr();
end
endmodule : top
//Output:
// (Compilation Error) Illegal to access non-static property 'j' from a static method.
Static methods cannot be virtual.
class A;
int unsigned j;
virtual static task incr();
$display("J is %d",j);
endtask
endclass
//Output:
// (Compilation Error) 'virtual' and 'static' qualifiers cannot be used on the same method
class A;
int unsigned j;
virtual static task incr();
$display("J is %d",j);
endtask
endclass
//Output:
// (Compilation Error) 'virtual' and 'static' qualifiers cannot be used on the same method
class A;
static task who();
$display("I am static method");
endtask
endclass
module top;
A a;
initial begin
// The static methods can be accessed using class name.
A::who();
// The static methods can be used without creating an object of that type.
a.who();
end
endmodule : top
//Output:
// I am static method
// I am static method
Static Lifetime method
By default, class methods have automatic lifetime for their arguments and variables.
All variables of a static lifetime method shall be static in that there shall be a single variable corresponding to each declared local variable in a class , regardless of the number of concurrent activations of the method.
All variables of a static lifetime method shall be static in that there shall be a single variable corresponding to each declared local variable in a class , regardless of the number of concurrent activations of the method.
By default, class methods have automatic lifetime for their arguments and variables.
Verilog-2001 allows tasks to be declared as automatic, so that all formal arguments and local variables are stored on the stack. SystemVerilog extends this capability by allowing specific formal arguments and local variables to be declared as automatic within a static task, or by declaring specific formal arguments and local variables as static within an automatic task.
class A;
task static incr();
int unsigned i; //static variable
$display("i is %d",i);
i++;
endtask
endclass
module top;
A a;
A b;
initial begin
$display("Static lifetime - non static task with static variables");
a = new();
b = new();
a.incr();
b.incr();
a.incr();
b.incr();
a.incr();
$display("Static lifetime - Each call to task will use a single value of 'j' and increment it");
end
endmodule : top
//Output
// Static lifetime - non static task with static variables
// i is 0
// i is 1
// i is 2
// i is 3
// i is 4
// Static lifetime - Each call to task will use a single value of 'j' and increment it
Finally difference between Static Method and Static Lifetime is
Static Method refers to the lifetime of the method within the class
while
Static Lifetime refers to the lifetime of the arguments and variables within the task