# Inline static object initialization

**URL:** <https://verificationacademy.com/forums/t/inline-static-object-initialization/30660>\
**Category:** SystemVerilog\
**Tags:** UVM, SystemVerilog, object, initialization, static\
**Created:** [May 20, 2015, 7:49am UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660 "2015-05-20T07:49:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fitc](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@Fitc](https://verificationacademy.com/forums/u/Fitc)\
**Post date:** [May 20, 2015, 7:49am UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/1 "2015-05-20T07:49:17Z")

</div>

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

```verilog
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

```verilog

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.

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [May 20, 2015, 12:33pm UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/2 "2015-05-20T12:33:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fitc](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@Fitc](https://verificationacademy.com/forums/u/Fitc)\
**Post date:** [May 20, 2015, 1:07pm UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/3 "2015-05-20T13:07:43Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/2):_

Is it possible to write component like below?

```verilog
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

```

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [May 20, 2015, 1:42pm UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/4 "2015-05-20T13:42:12Z")

</div>

_In reply to [Fitc](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/3):_

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.

---

<div class="post-metadata">

**Author:** ![Fitc](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@Fitc](https://verificationacademy.com/forums/u/Fitc)\
**Post date:** [May 20, 2015, 2:38pm UTC](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/5 "2015-05-20T14:38:18Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/inline-static-object-initialization/30660/4):_

Ok. Thank you
