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. If you need to access a static property inside a class, you can also use the magic keywords "this" and "super", which resolve to the current class and the parent of the current class, respectively. Using "this" and "super" allows you to avoid having to explicitly reference the class by name.
EXAMPLE: Using object name class A ;
staticint i;
endclass
program main ;
A obj_1;
A obj_2;
initial begin obj_1 = new();
obj_2 = new();
obj_1.i = 123;
$display(obj_2.i);
end endprogram
RESULT
123
The static class properties can be accessed using class name.
EXAMPLE: using class name class A ;
staticint i;
endclass
program main ;
A obj_1;
initial begin obj_1 = new();
obj_1.i = 123;
$display(A::i);
end endprogram
RESULT
123
The static class properties can be used without creating an object of that type.
EXAMPLE: without creating object class A ;
staticint i;
endclass
program main ;
A obj_1;
A obj_2;
initial begin obj_1.i = 123;
$display(obj_2.i);
end endprogram
RESULT
123
EXAMPLE: using the object name, without creating object class A ;
staticint i;
endclass
program main ;
A obj_1;
initial begin obj_1.i = 123;
$display(A::i);
end endprogram
RESULT
123
Static Methods
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.
EXAMPLE class A ;
statictask incr();
int j; //automatic variable
j++;
$display("J is %d",j);
endtask endclass
program main ;
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 endprogram
RESULT
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.
EXAMPLE class A ;
int j;
statictask incr();
j++;
$display("J is %d",j);
endtask endclass
program main ;
A obj_1;
A obj_2;
initial begin obj_1 = new();
obj_2 = new();
obj_1.incr();
obj_2.incr();
end endprogram
RESULT
A static method has no access to nonstatic members (class properties or methods).
Static methods cannot be virtual.
EXAMPLE class A ;
int j;
virtualstatictask incr();
$display("J is %d",j);
endtask endclass RESULT
Error : Static methods cannot be virtual.
The static methods can be accessed using class name.
EXAMPLE: using class name class A ;
statictask who();
$display(" Im static method ");
endtask endclass
program main;
initial A.who();
endprogram
RESULT
Im static method.
The static methods can be used without creating an object of that type.
EXAMPLE: without creating object class A ;
statictask who();
$display(" Im static method ");
endtask endclass
program main;
A a;
initial a.who();
endprogram
RESULT
Im 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.
EXAMPLE class A ;
taskstatic incr();
int i; //static variable
$display(" i is %d ",i);
i++;
endtask endclass
program main;
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 endprogram
RESULT
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
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.
By default, class methods have automatic lifetime for their arguments and variables.
EXAMPLE class packet;
staticint id;
//----static task using automatic fields ---//
statictask packet_id();
int count; // by default static task fields are automatic
id=count; // writing in to static variable
$display("id=%d count=%d",id,count);
count++;
endtask
function new();
int pckt_type;
pckt_type++;
$display("pckt_type=%d",pckt_type);
endfunction
SystemVerilog allows 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.
EXAMPLE class packet;
staticint id,pckt_type;
//---static task with static field----//
statictask packt_id();
staticint count; //explicit declaration of fields as static
id=count; //writing in to static variable
$display("id=%d count=%d",id,count);
count++;
endtask
function new();
pckt_type++;
$display("pckt_type=%d",pckt_type);
endfunction
endclass
program stic_2;
packet jumbo,pause,taggd;
initial begin jumbo=new();
jumbo.packt_id();
pause=new();
pause.packt_id();
taggd=new();
taggd.packt_id();