OOP

class trans;
    static int pr;
    
    function int mult(int a, int b); 
      pr = a*b; 
      return pr;
    endfunction
    
  endclass
  trans t1,t2;
  
module top;
  initial
    begin
      t1 = new();
      t1.pr = t1.mult(2,3);
      t2.pr = t2.mult(3,4);    // Will there be an error here as t2 handle access the method without an instance or is it legal?
      $display("t1.pr = %od and p2.pr = %0d",t1.pr,t2.pr);
    end
endmodule

In reply to Sinduja Kumarasamy:

Hello ma’am,
After seeing this code, I immediately thought it’d throw an error.But after running it on eda-playground , I observed that it’s a different case. We will get a proper answer because the member we are dealing with is static and for methods, we don’t need to create constructor.
I am not 100% sure about my statement . I made few changes to this code and came to this conclusion. I am sharing the code and the result as well. Please , Go through it.


class trans;
   static int pr;
 // int pr;  // it'll show error
    function int mult(int a, int b); 
       mult = a*b; 
      //return result;
      $display("%0d",mult);
    endfunction
 
  endclass
  trans t1,t2;
 
module top;
  initial
    begin
     // t1 = new();
      t1.mult(2,3);
      t2.pr = t2.mult(3,7);    // Will there be an error here as t2 handle access the method without an instance or is it legal?
      $display("t1.pr = %0d and p2.pr = %0d",t1.pr,t2.pr);
    end
endmodule

RESULT:-


# Loading sv_std.std
# Loading work.testbench_sv_unit(fast)
# Loading work.top(fast)
# 
# vsim -voptargs=+acc=npr
# run -all
# 6
# 21
# t1.pr = 21 and p2.pr = 21
# exit
# End time: 11:30:31 on Apr 22,2023, Elapsed time: 0:00:01
# Errors: 0, Warnings: 1

In reply to Sinduja Kumarasamy:

https://verificationacademy.com/forums/systemverilog/why-my-code-not-failing-when-accessing-non-static-method-not-static-class#reply-40439

In reply to Shubhabrata:

Thanks,Shubhabrata and Dave!