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();