“STATIC Properties and STATIC Methods”

STATIC Properties:

As we know that in SystemVerilog Class Properties do not get created until the Object gets constructed. But there is an exception to that. When we add a “static” modifier to a Property that Property becomes a Static variable and it is Global to the Class type. It is created as soon as we create the type and it’s available during the entire simulation. We access Static Class Properties, not by Handle but using the Scope resolution operator i.e. “::”. One more important thing to note about Static Properties is that they’re visible simultaneously to all the instances of the Class

Let’s see below an example related to Static Properties:

class Packet;
  int id;
  static int pkt_id;
endclass: Packet
 
program main;
  Packet dynapkt [];
 
 initial begin
   dynapkt = new[5];
   foreach (dynapkt[i]) begin
     dynapkt[i] = new();
     dynapkt[i].id = Packet::pkt_id++;
     $display("dynapkt[%0d].id = %0d", i, dynapkt[i].id);
   end
   
   for (int i = 0; i<dynapkt.size(); i++) begin
     $display("dynapkt[%0d].pkt_id = %0d", i, dynapkt[i].pkt_id);
   end
 end
 
endprogram

Here is Class “Packet” is having two Properties. One of them i.e. “id” is an integer and another one i.e. “pkt_id” is a Static Property. A dynamic array of type Packet Class is declared and later five Objects are constructed. After that using the Scope resolution operator (::) id value is assigned to each Object. In the end, the “for” loop shows that the Static Property pkt_id is reflected in all the instances of the Packet Class.

STATIC Methods:

In SystemVerilog, we can create a Static Method inside a Class that can only read & write Static variables, even before any Object of that Class is constructed. Non-Static variables are not allowed inside the Static Methods.

Let’s understand through the below example:

class Packet;
  int id;
  static int pkt_id;
  static function int PKT_ID;
     return PKT_ID++;
  endfunction: PKT_ID
endclass
  program main;
    Packet dynapkt [];
      initial begin
      dynapkt = new[5];
      foreach (dynapkt[i]) begin
        dynapkt[i] = new();
        dynapkt[i].id = Packet::PKT_ID;
        $display("dynapkt[%0d].id = %0d", i, dynapkt[i].id);
      end
    end
  endprogram

In the above example, we defined a Static function i.e. PKT_ID which returns the incremented value of a Static variable called pkt_id. And PKT_ID is used with the scope resolution operator to assigned the incremented values to the non-static integer variable i.e. id of the created Objects.

About the author

The Art of Verification

Hi, I’m Hardik, and welcome to The Art of Verification.

I’m a Verification Engineer who loves to crack complex designs and here to help others commit to mastering Verification Skills through self-learning, System Verilog, UVM, and most important to develop that thought process that every verification engineer should have.

I’ve made it my mission to give back and serve others beyond myself.

I will NEVER settle for less than I can be, do, give, or create.

View all posts

2 Comments

  • static function int PKT_ID;
    pkt_id++;
    return pkt_id ;
    endfunction: PKT_ID
    endclass

    There is a small mistake. The output of static function will be 1,2,3,4,5

  • Hi Hardik,
    Firstly, Thank you for the blogs. Really nice.

    I tried to run the 2nd chunk of code (static method). Saw the below output.
    # dynapkt[0].id = 0
    # dynapkt[1].id = 0
    # dynapkt[2].id = 0
    # dynapkt[3].id = 0
    # dynapkt[4].id = 0
    (Tool: Questa)

    I am not able to ascertain the reason. Can you please explain that?