top of page

8.2. Constructor in System Verilog (new())

​

Constructor is used to create the object of the class. This is the default function in every class with name “new()”. 

​

This is different from “new[]” operator. This is used to provide the size of Dynamic Array.

​

A constructor’s primary role is to initialize the class properties. Once the object is created using “new()” and the variables are initialized, the class handle becomes a valid object reference that points to the allocated memory for that instance.

​

If the explicit definition of the constructor is not created in the class, default definition is called which initializes the variables implicitly.

​

Below is the example with implicit constructor call:

Verilog

In the above code, when “p1 = new()” is executed then variable of the Packet class gets the default value of int type i.e., “addr = 0”. 

​

Below is the example with explicit constructor call:

Verilog

In the above code, when “p1 = new()” is executed then variable of the Packet class gets the explicit value 32’b0 as mentioned in the new() function i.e., “addr = 0”.

​

Below is the example with explicit constructor call with Arguments:

Verilog

In the above code, when “p1 = new()” is executed with the argument (value = 32’hDEADDEED) then variable of the Packet class gets the argument value 32’hDEADDEED as mentioned in the new() function i.e., “addr = def_val”.​
 

Industry standard example:

Verilog

The important Interview question comes from constructor:

​​

Can a constructor be a virtual method in SystemVerilog?

​

Direct answer is “NO”. In SystemVerilog, a constructor (new()) cannot be declared as “virtual”. This is because a constructor can not be overridden polymorphically, and SystemVerilog does not allow dynamic dispatch for new(). 

​

First, let’s understand what “virtual” means. 

​

virtual = “runtime decision”

​

virtual method = “A virtual method allows the simulator to decide at runtime whether to call the base or derived implementation based on the actual object type.”

Verilog

A virtual method requires a valid object reference to perform runtime dispatch.

​

But a constructor’s purpose is to create the object itself.

​

So the sequence is:

​

  1.new() is called

​

  2.object memory is allocated and initialized

 

  3.handle becomes a valid object reference

​

  4.virtual method dispatch can happen

​​

​

If new() were virtual, the simulator would need an already-created object to decide which constructor to call — which is not possible.

​

Therefore, virtual constructors are illegal in SystemVerilog, and the simulator reports a compile-time error.

Class fundamentals

Static and Dynamin member

© Copyright 2025 VLSI Mentor. All Rights Reserved.©

Connect with us

  • Instagram
  • Facebook
  • Twitter
  • LinkedIn
  • YouTube
bottom of page