New() vs create()

class driver extends uvm_driver
`uvm_component_utils (driver)

function new(string name,uvm_component parent);
super.new(name,parent);
endfunction

function void build_phase(uvm_phase phase)
super.build_phase(phase)
agent=agent::type_id::create(“agent”);
endfunction

I am confused in that whether instead of using new function for creating a constructor can we create using create()?I mean in the above code either of two can be used.Is that possible?.What is the difference between build phase and new function?

PS:Sorry if it is a too lame question to be asked.

The create function goes through the UVM factory and checks for registered type or instance overrides. This allows for objects or components to be replaced by derived types using the factory. I would suggest reading up on Factory Pattern, which is a key concept in UVM.

The new function is a SystemVerilog constructor for an object and is called everytime an object is to be created (whether through the factory or not).

In reply to Ayush:

As I explain in the previous thread where you asked this question, there is a difference between the code that calls for the construction of a class object, and the code that implements the construction of a class object.

The code that calls for the construction of another class object has a choice of calling new() or create() depending on whether the class you want to construct is registered with the factory. You need to search for material that describes the UVM factory.

Then there is the code you want use that implements the construction of a class object, which is what you normally put inside the body of the new() method. For classes derived from uvm_component, we suggest that you put the minimum amount of code required by SystemVerilog, like calling super.new(), and put the rest of the code in the build_phase(). Then, if and when you extend that class, you can override the code in the build_phase to do something different. You can’t do that with code in the new() method because you are required to call super.new()

In reply to dave_59:

Thanks Dave