Access of Class method without creating Object

Hi Dave,

I have below query regarding class.

As, per my understanding class is dynamic entity.
So, we can only able to access class method and property when its object created using class constructor new().
like object.method()
In below example I have declare only handle of class and not created memory for class.
But, still my code is working fine.



class a;
  task t();
    $display("Class A");
  endtask : t
endclass : a

class b;
  function int t (int a,b);
    $display("Class B -> a + b = %0d",(a+b));
    return a+b;
  endfunction : t
endclass : b

module m;
  a a1;
  b b1;
  int c;
  
  initial begin
    a1.t();
    c = b1.t(5,5);
    $display("Value of C = %0d",c);
  end
  
endmodule : m

Result :-
 
Expected
 Null pointer error as object is not created.

Actual 
# vsim -voptargs=+acc=npr
# run -all
# Class A
# Class B -> a + b = 10
# Value of C = 10
# exit
# End time: 07:37:31 on Mar 24,2021, Elapsed time: 0:00:00
# Errors: 0, Warnings: 1


I have checked LRM. I did’t find any relevant topic.
Can you guide me why this code work well even I have not created memory/Object for class.
You can refer same example on EDA playground.

Thanks,
Harsh

In reply to harsh pandya:

In the class methods you are not accessing any class properties and the methods are not virtual, so you can call those methods without creating objects.

But in case if you are accessing class properties inside the methods & if the methods are virtual then first we need to create the object before calling the methods.

In reply to shanthi:

In reply to harsh pandya:
In the class methods you are not accessing any class properties and the methods are not virtual, so you can call those methods without creating objects.

Thanks for your feedback.

That, I know if we try to access class method which includes class property we meet to “Bad handle Reference” ( below example ).


// We got Error when we try to access method using b1.t() and object is not created.
class b;
  int x = 10;
  function int t (int a,b);
    $display("Class B -> a + b = %0d",(a+b+x));
    return a+b+x;
  endfunction : t
endclass : b

But, I want to know if class property not included in class method how it is working when object is not created.

But in case if you are accessing class properties inside the methods & if the methods are virtual then first we need to create the object before calling the methods

I am not sure this is related to virtual or non virtual. with virtual I am understanding method not present actually. So, if we try to access we got error.

Btw, can you provide any LRM reference for same.

Thanks
Harsh

In reply to harsh pandya:

See Why is my code not failing when accessing non static method from not static class? | Verification Academy

In reply to dave_59:

In reply to harsh pandya:
See Why is my code not failing when accessing non static method from not static class? - SystemVerilog - Verification Academy

Thanks for your reply dave.

As, I understand from above link is describe below.

  1. Non static property or virtual method can not be access by null object as per LRM.

In my example


class a;
  task t();
    $display("Class A");
  endtask : t
endclass : a
....
....
a.t();
.....
.....

//equal to
class a;
endclass : a
task a_t();
  $display("Class A");
endtask
........
........
a_t();
........
........

So, above method act as independent method instead of class depending method.
As, per LRM snapshot you shared specified rules for non static property and virtual method.
So, static property and non virtual method via null object is EDA vendor specific.
Is my understanding is correct.

Thanks,
Harsh