Tutorials

Learn More

A static keyword is used in a class member to denote class has static properties or static methods.

Static Properties (static variables)

The static variable declared inside a class with static keyword shares a single memory location across all class instances.

static <data type> <variable name>

Static properties example

In the below example, we will have two variables “static int s_id” (static variable) and “int id” (non-static variable). There are 5 instances of a class that has been created and both variables are incremented by 1 in the constructor method of a class. The intention is to have a unique id for each class instance.

class transaction;
  static int s_id;
  int id;
  
  function new ();
    s_id++;
    id++;
  endfunction
endclass

module class_example;
  transaction tr[5];
  initial begin
    foreach (tr[i]) begin 
      tr[i] = new();
      $display("Value of s_id = %0h, id = %0h", tr[i].s_id, tr[i].id);
    end
  end
endmodule

Output:

Value of s_id = 1, id = 1
Value of s_id = 2, id = 1
Value of s_id = 3, id = 1
Value of s_id = 4, id = 1
Value of s_id = 5, id = 1

For unique value as per requirement, a user should use a static keyword in such a scenario.

Static Methods (static functions and static tasks)

Static methods are the same as static variables that also follow class access rules and scope.

  1. Static functions and tasks can not be virtual
  2. They can access only static properties (static members) of a class. Accessing non-static members leads to compilation errors as it is illegal to use. But non-static functions or tasks can access static variables.
  3. Both static methods and static members in a class can be accessed without creating an object.

Static Methods examples

Example with object creation

class transaction;
  static int s_id;
  int id;
  
  static function void incr_s_id(); // Static function
    s_id++;
    //id++; // illegal access
  endfunction
  
  function void incr_id(); // Non-static function
    s_id++;
    id++;
  endfunction
  
endclass

module class_example;
  transaction tr[5], tr_new;
  initial begin
    foreach (tr[i]) begin 
      tr[i] = new();
      tr[i].incr_s_id();
      $display("On calling incr_s_id: Value of s_id = %0h, id = %0h", tr[i].s_id, tr[i].id);
    end
    tr[0].s_id = 0; // s_id value reset to 0
    foreach (tr[i]) begin 
      tr[i].incr_id();
      $display("On calling incr_id: Value of s_id = %0h, id = %0h", tr[i].s_id, tr[i].id);
    end
  end
endmodule

Output:

On calling incr_s_id: Value of s_id = 1, id = 0
On calling incr_s_id: Value of s_id = 2, id = 0
On calling incr_s_id: Value of s_id = 3, id = 0
On calling incr_s_id: Value of s_id = 4, id = 0
On calling incr_s_id: Value of s_id = 5, id = 0
On calling incr_id: Value of s_id = 1, id = 1
On calling incr_id: Value of s_id = 2, id = 1
On calling incr_id: Value of s_id = 3, id = 1
On calling incr_id: Value of s_id = 4, id = 1
On calling incr_id: Value of s_id = 5, id = 1

Example without object creation

Static function incr_s_id can be called in two different ways.

A. Using scope resolution operator
Syntax: <class_name>::<static method>
Ex: transaction::incr_s_id();
transaction::s_id

Note: Refer Scope Resolution Operator to know more.

B. Using class handle
Syntax: <instance_name>.<static method>
Ex: tr.incr_s_id();
tr.s_id;

    In the below example, incr_s_id is called in both ways, static variable s_id will be incremented twice. Later, an object is created to print values.

    class transaction;
      static int s_id;
      
      static function void incr_s_id(); // Static function
        s_id++;
      endfunction
      
    endclass
    
    module class_example;
      transaction tr;
      initial begin
        transaction::incr_s_id(); // Access static function without class handle
        tr.incr_s_id(); // Access static function with class handle
        $display("After incr_id function call");
        $display("Value of s_id = %0h using tr handle", tr.s_id);
        $display("Value of s_id = %0h using scope resolution operator", transaction::s_id);
      end
    endmodule

    Output:

    After incr_id function call
    Value of s_id = 2 using tr handle
    Value of s_id = 2 using scope resolution operator

    function static or task static method call

    It is now clear that non-static class members can not be accessible from the static method. What if the user wants to access automatic (non-static) members of the class. It is possible to use the “function static” method call. This is also known as a non-static method call with a static variable lifetime. On calling “function static”, variables declared in function will be static.

    Note:

    // static function
    static function void incr_s_id();
    ...
    endfunction
    
    // Non-static function and variables declared in function will be static.
    function static int incr_id();
    ...
    endfunction

    function static call Example

    class transaction;
      static int s_id;
      int id;
      
      function static int incr_id(); // Non-static function
        int status; // Behaves as a static variable.
        status = s_id++;
        id++; // non-static member is accessible
        return status;
      endfunction
    endclass
    
    module class_example;
      transaction tr[5];
      int local_status;
    
      initial begin
        foreach (tr[i]) begin 
          tr[i] = new();
          local_status = tr[i].incr_id();
          $display("Value of s_id = %0h, id = %0h, local_status = %0h", tr[i].s_id, tr[i].id,  local_status);
        end
      end
    endmodule

    Output:

    Value of s_id = 1, id = 1, local_status = 0
    Value of s_id = 2, id = 1, local_status = 1
    Value of s_id = 3, id = 1, local_status = 2
    Value of s_id = 4, id = 1, local_status = 3
    Value of s_id = 5, id = 1, local_status = 4

    System Verilog Tutorials