|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



STATIC


Static Class Properties




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 ;
static int 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 ;
static int 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 ;
static int 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 ;
static int 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 ;
static task 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;

static task 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;

virtual static task 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 ;
static task 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 ;
static task 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 ;

task static 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;
static int id;
//----static task using automatic fields ---//
static task 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


endclass

program stic_1;
packet jumbo,pause,taggd;

initial
begin

jumbo=new();
jumbo.packt_id();
pause=new();
pause.packt_id();
taggd=new();
taggd.packt_id();

end
endprogram

RESULTS

pckt_type= 1; id= 0; count= 0
pckt_type= 1; id= 0 ; count= 0
pckt_type= 1; id= 0; count= 0




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;
static int id,pckt_type;
//---static task with static field----//
static task packt_id();
static int 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();

end
endprogram

RESULTS

pckt_type= 1; id= 0 count= 0;
pckt_type= 2; id= 1 count= 1;
pckt_type= 3 ; id= 2 count= 2;

Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer