What is meant by a static class?

If i say
“local static ‘class name’ inst ;”
What does it mean ?

In reply to munish kumar:

Referring to IEEE 1800-2012 section 8.18:
local:

A member identified as local is available only to methods inside the class. Further, these local members are not visible within subclasses.

It is a from of data hiding. Sometimes, it is desirable to restrict access to class properties and methods from outside the class by hiding their names.

Referring to IEEE 1800-2012 section 8.9:
static:

The use of a static SystemVerilog variable implies that only one copy exists. A static method is subject to all the class scoping and access rules, but behaves like a regular subroutine that can be called outside the class, even with no class instantiation.

The above syntax, local static class inst, is used to create a singleton. Singleton is an object which can be shared among all the instances.

class A;
local static A a_singleton;
static function A get(); // function to return singleton handle
if(a_singleton == null)
  a_singleton = new; // create a_singleton once only
return a_singleton;
endfunction

class other_class; // many other classes can access single object using get method
A mya;
function some_method();
mya = A::get(); // get singleton handle
endfunction

This method is used in methodologies like UVM. For example, there is a single instance of uvm_root and uvm_factory library classes.

In reply to sharvil111:

Thanks for the answer!!

In reply to munish kumar:
A terminology clarification:

There is no such thing as a static class in SystemVerilog - all classes are dynamically constructed. What this code is doing is declaring a static variable that can hold a handle to a class object.

You may want to take a look at my SystemVerilog OOP course that covers the use of the local and static keywords, as well as the singleton pattern.

In reply to dave_59:

Hi Dave ,

For the below Code I have a class with static property “name” , in the Code below I never Call the Constructor using new , so how is the Class Constructed dynamically ?


module Static_Property_In_Parametrized_Class2;
  
class B #( type T = int , T w = 1 );

static string name = get();

// function string get(); // Error :: Class instance variable required to call non-static method 'get'

static function string get();
  
name =  $sformatf("packet%0d",w);
$display($time, " Type_Parameter = %0s name == %0s",$typename(T),name); 
  
endfunction  

endclass

 
 
typedef B #( .T(bit[1:5]) , .w(3) ) w3;

w3 a1,a2;

B #(real,10.55) b1;

`ifdef T

B #( .T(bit[1:5]) , .w(3) ) a9;

`endif

initial begin

#10;
$display($time," After Delay ");

$display( B#(int,10)::name );

$display( B#()::name );

$display($time," %0s", a1.name );

end   

Also the Output (With OR Without +define+T )of the above Code is as follows ::

0 Type_Parameter = real name == packet11

0 Type_Parameter = bit[1:5] name == packet3

0 Type_Parameter = int name == packet1

0 Type_Parameter = int name == packet10

10 After Delay

10

I have a few queries regarding the Output

Q1] How is it that I get " Type_Paramter = real " Before " Type_Parameter = bit[1:5] " ( without +define+T ),
Does it have to do Elaboration time ( typedef ) and Compilation time ( Class Handle b1 ) ?

Q2] " A static property is Never Created until the class becomes a concrete specialization "

I understand that I have created a concrete specialization using typedef w3 and b1 , a9 Since I instantiate the class variable

Do the statements " $display( B#(int,10)::name ); $display( B#()::name ); " create a Specialized Class as well ? Since I neither use a typedef Nor Declare a class Variable ( like a9 )

Q3] initial block Works Sequentially so shouldn’t I get the output of
" $display( B#(int,10)::name );$display( B#()::name ); " after Time = 10 ?

Q4] +define+T
Since a1 , a2 are Not Unique Parameters I don’t get Displays for it in the output , Same is the case with w3 since we already have a9 , But I expect " packet3 " at Time = 10 Using a1.name as Argument but its not the case

In reply to Etrx91:

A1) the execution order of static variable initializations is undefined. This is why we usually declare static variables as ‘local’ and use a method like get() to initialize it on first use.

A2) Any reference to a parameterized class creates a specialization if one does not already exist.

A3) All references get elaborated at compile time, and all static variables get initialized before time 0.

A4) See A2

In reply to sharvil111:

Hi,
Can you please explain why we need class name before get function in the singleton class code, like:

static function A get(); // function to return singleton handle.

Why can’t we write it simply like :

static function  get(); // function to return singleton handle.

Thanks & Regards
Vineet

In reply to vineet:

The return data type of any function is written right after the function keyword. If you do not specify a type, then the Verilog default return type is 1 bit.

In reply to dave_59:

Hi Dave ,

Could you please explain answer A3 above ?

" all static variables get initialized before time 0. "

We have 3 stages : Compilation , Elaboration and Simulation .

When exactly are the static Variables initialized ( Out of the above 3 Stages ) ?

A regression is run without re-compiling the design and testbench .
Just be changing Argument to +UVM_TESTNAME , the design can be re-simulated .

When does the factory registration take place ?
Can I say simulation is also divided into parts . One where Static Properties are Initialized and the other where simulation runs ?

Thanks in advance

In reply to Have_A_Doubt:

I use the term “before time 0” to mean before any process created by initial, always, continuous assignments, etc. start. You can call this part of the start of simulation stage; however, the LRM does not define this. If a tool chose to initialize certain static variables at the end of elaboration, there is no way for you to know the difference.

In reply to dave_59:

Hi Dave , you said that all classes are dynamically constructed.

if the class is never constructed, that class’s static variables still get initialized before time 0?
is it true?
if true, will it waste memory resource?
Thanks!

In reply to peter:

static class variables are initialized before time 0, like any other non-class static variable. They do not have dynamic lifetimes.

“waste” is a subjective term. There is only one static variable per class type, regardless of the number of instances of that type.