SystemVerilog Static class properties and Ststic methods

 Static class properties and Static methods 


Static class properties are created using the keyword static. static properties will be created once and all the class objects will access the same.

static methods are same as static properties, static method can access only static properties of the class and access to the non-static properties is illegal and lead to compilation error.

Note :: static class properties and methods can be used without creating an object of that type.


 Example -1 

The below example shows,
access to static properties and static methods,and access to static class properties and methods are accessed without creating an object.

class packet;
  //class properties
  bit [31:0] addr;
  bit [31:0] data;
  bit        write;
  string     pkt_type;
  bit        pkt_id;

  //static property to keep track of number of pkt's created
  static int no_of_pkts_created;

  //constructor
  function new(bit [31:0] addr,data,bit write,string pkt_type);
    this.addr  = addr;
    this.data  = data;
    this.write = write;
    this.pkt_type = pkt_type;
    //increment pkt count on creating an object
    no_of_pkts_created++;
    pkt_id     = no_of_pkts_created;
  endfunction

  //method to display class properties
  function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction

  //static method to display next_pkt id
  static function void next_pkt_id;
    $display("---------------------------------------------------------");
    $display("\t no of pkt's created  = %0d",no_of_pkts_created);
    $display("\t next pkt id  = %0d",no_of_pkts_created+1);
    $display("---------------------------------------------------------");
  endfunction
endclass

module static_properties_method;
  packet pkt[3];
  packet pkt_2;

  initial begin
    foreach(pkt[i]) begin
      pkt[i] = new(32'h10*i,32'hF*i,1,"GOOD_PKT");
      pkt[i].display();
    end
    pkt[1].next_pkt_id;
    //static class properties and methods are accessed without 
    //creating an object for pkt_2 handle.
    pkt_2.next_pkt_id;
  end
endmodule


 Simulator Output  
---------------------------------------------------------
addr = 0
data = 0
write = 1
pkt_type = GOOD_PKT
---------------------------------------------------------
---------------------------------------------------------
addr = 10
data = f
write = 1
pkt_type = GOOD_PKT
---------------------------------------------------------
---------------------------------------------------------
addr = 20
data = 1e
write = 1
pkt_type = GOOD_PKT
---------------------------------------------------------
---------------------------------------------------------
no of pkt's created = 3
next pkt id = 4
---------------------------------------------------------
---------------------------------------------------------
no of pkt's created = 3
next pkt id = 4
---------------------------------------------------------
Execute the above code on