Inline static object initialization

Hi,
How to initialize static object’s fields once?
I have an item class:

class some_item extends uvm_sequence_item;
  function new (string name = "some_item");
    super.new(name);
  endfunction
  
  int item_type=0;
  //...
endclass

and component class


class some_component extends uvm_component;
  static some_item item_1=new("item_1"); //want to assign "1" to item_type once
  static some_item item_2=new("item_2"); //want to assign "2" to item_type once
  //...
endlass

Field “item_type” in both items should be initialized once.

You should always use the UVM create() method for any component or object extended from the UVM base component or object. If you need to initialize some internal variables, it is recommended to do that in the build() phase.

If there is a need to override some internal variables, use the config_db to provide those overrides on an individual basis.

In reply to cgales:

Is it possible to write component like below?

class some_component extends uvm_component;
  static some_item item_1;
  static some_item item_2;

  function void build_phase(uvm_phase phase);
    if(item_1==null) begin
      item_1=some_item::type_id::create("item_1");
      item_1.item_type=1;
    end
    if(item_2==null) begin
      item_2=some_item::type_id::create("item_2");
      item_2.item_type=2;
    end
  endfunction
endlass

In reply to Fitc:

Yes, this is valid per the LRM, but it’s not very flexible. Creating a static class instance will result in every instance of some_component having the same item_1 and item_2. While this may be something that is desired, it is better to have each instance of some_component get the handle for item_1 and item_2 from the config_db(). By doing this, you could assign the same handles to every some_component (similar to a static instantiation), or you have the flexibility to provide different handles.

Also, assigning internal variables directly (item_1.item_type=1) violates the basic rules of object oriented programming in that you should only access internal variables via public methods.

In reply to cgales:

Ok. Thank you