top of page

Article 13 (Part 1): System Verilog Classes — Objects, Handles, and new() Explained Properly

1. Why Classes Exist (Not Syntax — Motivation)

Up to now, Alice and Bob have worked with:

  • Signals
     

  • Arrays
     

  • Structs
     

  • Queues
     

All of these share one limitation:

They describe data, not behavior over time.

Alice now wants to model things that act:

  • A transaction that can print itself
     

  • A packet that can compare itself
     

  • An object that can be passed around and modified
     

This is not hardware modeling.
This is verification modeling.

That is why classes exist.

2. What Is a Class? (Precise, Interview-Safe Definition)

A class in System Verilog is a blueprint for creating dynamically allocated objects that bundle data and behavior, accessed through handles and existing only in simulation.

Key words (do not skip):

  • Blueprint
     

  • Dynamically allocated
     

  • Objects
     

  • Handles
     

  • Simulation-only
     

If you can explain each word, you understand classes.

3. Alice–Bob Mental Model (Lock This In)

  • Alice writes a design for a thing → class
     

  • Bob creates real things → objects
     

  • Bob never touches the blueprint directly
     

  • Bob only interacts with objects via handles
     

This separation is fundamental.

4. Class ≠ Object (This Is Where Most Fail)

Very Important Distinction

class packet;

  int addr;

  int data;

endclass

 

This code:

  • ❌ does NOT create memory
     

  • ❌ does NOT create an object
     

  • ✅ only defines a type
     

Think of it as:

“Alice draws a blueprint. No house exists yet.”

5. Handles — What You Really Declare

packet p;

 

This line declares:

  • ❌ NOT an object
     

  • ✅ a handle
     

Interview-grade explanation:

A handle is a reference variable that can point to an object, similar to a pointer but safer.

At this point:

  • No memory is allocated
     

p points to nothing

6. new() — Why It Is Absolutely Critical

What new() Actually Does

p = new();

 

This line:

  • Allocates memory
     

  • Creates the object
     

  • Returns a reference
     

  • Assigns that reference to the handle
     

Alice–Bob View

  • Alice wrote the blueprint
     

  • Bob says: “Create one real object now”
     

That action is new()

Runnable Example — Missing new() Bug

module missing_new_example;

  packet p;

 

  initial begin

    p.addr = 10;   // ❌ runtime error

  end

endmodule

 

Why this fails:

  • p is just a handle
     

  • No object exists
     

  • You are dereferencing null
     

👉 This is one of the most common interview traps.

7. Full Correct Example — Handle + new()

class packet;

  int addr;

  int data;

 

  function void display();

    $display("addr=%0d data=%0d", addr, data);

  endfunction

endclass

 

module class_basic_example;

  initial begin

    packet p;

    p = new();      // object created

 

    p.addr = 10;

    p.data = 99;

 

    p.display();

  end

endmodule

 

Now:

  • Memory exists
     

  • Methods work
     

Behavior is attached to data

8. Why Classes Are Passed by Reference (Deep but Essential)

This Code Is Legal — and Dangerous If Misunderstood

module handle_aliasing_example;

  initial begin

    packet p1, p2;

 

    p1 = new();

    p1.addr = 5;

 

    p2 = p1;        // NOT a copy

    p2.addr = 20;

 

    p1.display();

  end

endmodule

 

What Actually Happened

  • Only one object exists
     

  • Two handles point to it
     

  • Changing via one affects the other
     

Alice–Bob Analogy

  • Alice gives Bob a remote control
     

  • Bob gives the same remote to Charlie
     

  • There is still one TV
     

👉 This is reference semantics, not value semantics.

9. Why This Design Choice Exists (Interview Depth)

System Verilog chose reference semantics because:

  • Copying objects blindly is expensive
     

  • Verification objects are often shared
     

  • Performance and flexibility matter more than safety
     

But it means:

  • You must be conscious of aliasing
     

Bugs are logical, not syntactic

10. Class vs Struct (Now You Can Truly Answer This)

Aspect           | Class                          | Struct
Allocation       Dynamic (new)           Static
Lifetime          Runtime-controlled   Scope-controlled
Methods          Yes                               No
Passing           Reference                   Value
Use case         Behavior + data          Data only

Now this comparison actually makes sense.

11. Common Interview Questions — Now Answerable

After this article, you should be able to answer:

  • Why is new() required?
     

  • What happens if new() is missing?
     

  • What is a handle?
     

  • Why does assignment not copy objects?
     

  • Why are classes not synthesizable?
     

  • Difference between class and struct?
     

If any of these still feel vague → reread Sections 4–8.

12. One Sentence to Remember (Interview Gold)

A class defines behavior, new() creates objects, and handles reference those objects.

If this sentence is clear, you’re solid.

13. In next article , We will cover:

  • Inheritance
     

  • Polymorphism
     

  • Virtual methods

bottom of page