Need clarification on static method and non static method

what is the difference between static class method with automatic variable lifetime and non static class method with static variable lifetime and why we are creating objects for non static class method.

eg: static task foo(); // static class method with automatic variable lifetime

endtask

task static foo(); // non static class method with static variable lifetime 
.......
endtask

In reply to Somu:

See if this post and the links it provides helps you.

Hi Dave,

Thanks for the link, but i have doubts

task static foo();
    .......
   endtask

here it is non static class method with static variable lifetime, here why we are saying non_static class method, what does it really mean??

module test;

     class name;
    
        int j; //automatic variable

        task static foo();
           j++;
           $display("j=%0d",j);
        endtask

     endclass
     
        initial begin
           name n;
           n = new;
           n.foo;
           n.foo;
        end
  endmodule

here why we need to create object for the class, if i am not creating object it is showing null handle dereference, as task is non_static but variables inside are having static lifetime,only for automatic variable we are creating object??

static task foo();
    ..........
   endtask

here it is static class method with automatic variable lifetime, why we call it is as static class method but inside this method variables are having automatic lifetime, can you please justify what does we really mean by static class method.

looking forward to your reply

Regards
Somu

In reply to Somu:
task static foo();

endtask

Is illegal inside a class. If you are not getting an syntax error, you are using a tool that does not support the current version of the SystemVerilog standard. SystemVerilog made this syntax illegal for the exact doubt you are having now. There is never a good reason to use this syntax outside a class, but SystemVerilog is required to be backward compatible with Verilog.

Class members (like j in your example) are not automatic variables. They are dynamically allocated when you call the class constructor
new()
. Automatic variable are allocated upon entry to a block like a task or function.

When you put the
static
keyword in front of a class method, it has nothing to do with the lifetime of the method - it is always automatic.

You should take a look at my short course on SystemVerilog OOP for more information of static methods.

In reply to dave_59:

Hi Dave,
Went through your explanation regarding “static task” & “task static” in different posts. I understood,

  1. Lifetime qualifier of class methods is by default Automatic
  2. “task static foo();” // static here is lifetime qualifier but is illegal inside class.
    Is legal and can be used outside the class.
  3. “static task foo();” // static is class qualifier, falls into the category of local and
    protected

But couldn’t understand what is the need of “static task foo()” which is having automatic lifetime. where it find its application, where can we use this.

Thanks

In reply to ragnomore:
I’ve rarely seen static tasks used in SystemVerilog, just static functions. The static class member qualifier means the variable or method is specific to the type, not a particular object. Typically you use a static method to access static variables that are local or protected from access outside the class.

I do explain this further in my SystemVerilog OOP course.

You can use static methods to create parameterized versions of tasks and functions without ever constructing an object of the class. Some tools will even synthesize these.

In reply to dave_59:

Thanks Dave.

In reply to Somu:

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn’t depend on the individual characteristics of its class, make it static (it will make the program’s footprint smaller). Otherwise, it should be non-static.

Example:

class Foo {
    int i;

    public Foo(int i) { 
       this.i = i;
    }

    public static String method1() {
       return "An example string that doesn't depend on i (an instance variable)";
    }

    public int method2() {
       return this.i + 1; // Depends on i
    }
}

You can call static methods like this: Foo.method1(). If you try that with method2, it will fail. But this will work: Foo bar = new Foo(1); bar.method2();

Does new() method belongs to the class itself or the object? I need clarification on that. @dave_59 @nicolbiden

I mean is it dynamic or automatic or static.? Or neither of these?

How memory is allocated for a methods inside a class and especially for new method. Of course object is dynamic, so the memory for the method space is created dynamically.

All class methods have automatic lifetimes. However, the keyword static has two different meanings depending on the context: one in relation to its lifetime and another in the context of classes, indicating whether it belongs to the class type or each dynamic instance of the class.

The special new() method of a class is a function with an automatic lifetime, but it is a static method of the class. You might want to see my DVCon presentation: The Life of a SystemVerilog Variable

1 Like

Hi @dave_59 Thanks for the reply. Now I understand it correcty. According to your response, that is why we can use a function like:


class def_class;

  function new();  
    // special case:
    // - Belongs to the CLASS (static in scope)
    // - Has AUTOMATIC lifetime for its locals
    // - Cannot be declared static or automatic explicitly
  endfunction: new  

  function funct_1();
    //  Belongs to each OBJECT instance
    //  Has AUTOMATIC lifetime (default)
    // So both ownership and lifetime are automatic
  endfunction: funct_1


  static function funct_2();
    //  Belongs to the CLASS (static in scope)
    //  Locals have AUTOMATIC lifetime (default)
    // To make locals static, you’d write “static int x;” *inside*.
  endfunction: funct_2


  function static funct_3();
    //  Belongs to each OBJECT instance
    //  Locals have static lifetime 
  endfunction: funct_3

  //A function does not persists between calls. The properties do


  static function static funct_4();
    //  Belongs to the CLASS (static in scope)
    //  Locals have  STATIC lifetime (retains value across calls)
  endfunction: funct_4

endclass: def_class

To avoid confusion with static class properties and static lifetimes, SystemVerilog prohibits the declaration of class methods with a static lifetime. These methods always have automatic lifetimes. Therefore, your last two examples are not legal.

1 Like