Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Start Here6
  • SystemVerilog Language Foundations28
  • Object-Oriented SystemVerilog for Verification30
    • Classes, Objects & Handles
    • Inheritance and Polymorphism
    • Static, Encapsulation & Parameterized Classes
      • Static Properties & Methods
      • local, protected & Encapsulation
      • Parameterized Classes
      • Constants & Enums in Classes
      • extern Methods & Class File Organization
    • Virtual Interfaces: the Class-to-Pins Bridge
    • Events, Semaphores, Mailboxes and Processes
    • OOP Patterns for Testbenches
  • Constraint Randomization — Interview-Critical Deep Dive36
  • SystemVerilog Assertions — Interview-Critical Deep Dive40
  • Functional Coverage in SystemVerilog22
  • Testbench Architecture in SystemVerilog23
  • Advanced Language & Integration25
  • Senior & Interview Preparation21
  • Principal-Level Verification28
Loading lesson

‹Static, Encapsulation & Parameterized Classes
  1. Topics
  2. SystemVerilog
  3. Object-Oriented SystemVerilog for Verification
  4. Static, Encapsulation & Parameterized Classes
  5. extern Methods & Class File Organization

Part 2 · OOP for Verification · Intermediate

extern Methods & Class File Organization

Prototypes inside the class with bodies outside, plus the one-class-per-file library.

On this page

    Why this matters

    A thousand-line class hid its own interface: finding which methods existed meant scrolling through every body. extern keeps the class readable as a list of prototypes.

    After this lesson you can: declare methods extern and define them with the class scope, and organise a class library as one package of included files.

    Prototype inside, body outside

    A long method body buried in the class hides the interface. The extern qualifier keeps only the prototype inside the class and moves the body to file scope, attached with the class scope operator.

    An out-of-body definition is semantically inside the class. It sees this handle, local members and protected members exactly as an in-body method does.

    Rule

    Prototype (in class)

    Definition (at file scope)

    Signature

    extern function int get_total();

    function int counter_ops::get_total();

    Qualifiers

    virtual, protected and static live here

    Never repeated here

    Scope

    Declares the API in one readable block

    Same scope as the class declaration

    Testbench

    One class split across a prototype block and scope-resolved bodies, proven through its local state.

    top.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    class counter_ops;
      local int total;
    
      // The class body reads like a table of contents.
      extern function new();
      extern function void add(int n);
      extern function int get_total();
    endclass
    
    // Out-of-body definitions: class_name::method_name.
    function counter_ops::new();
      total = 0;
    endfunction
    
    function void counter_ops::add(int n);
      total += n;   // local member: visible, this IS the class
    endfunction
    
    function int counter_ops::get_total();
      return total;
    endfunction
    
    module top;
      counter_ops c;
      int errors = 0;
      initial begin
        c = new();
        c.add(2);
        c.add(3);
        if (c.get_total() !== 5) errors++;
        if (errors == 0) $display("PASS: extern bodies added 2 and 3 for a total of 5");
        else $fatal(1, "FAIL: extern-built total differs from 5");
        $finish;
      end
    endmodule
    Terminal
    PASS: extern bodies added 2 and 3 for a total of 5

    File organization at scale

    A driver class with fifteen methods reads best as a 25-line declaration followed by bodies. Reviewers ask to see the class and mean the prototypes, which is also the style of the UVM library sources.

    The standard library shape below is structure, not compiled code here: one file per class, included once into a single package in dependency order.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    // snippet: multi-file library shape, shown as structure rather than compiled code
    // bus_pkg.sv: the only file on the compile list
    package bus_pkg;
    
      typedef class bus_driver;   // forward declaration when two classes name each other
    
      `include "bus_txn.sv"       // one class per file, dependency order
      `include "bus_driver.sv"     // extern bodies live with their class file
      `include "bus_env.sv"        // depends on everything above
    
    endpackage
    
    // In the testbench: import bus_pkg::*; then bus_env env = new();

    Never include one class file from two scopes: each inclusion declares a separate copy that the compiler treats as an unrelated type. Bodies that drive pins with forever and clock timing need a vif and a live interface, so they stay out of the checked single-file form.

    Common mistakes

    Signature drift between prototype and body fails compilation with errors that confuse beginners. Repeating virtual, static or protected on the definition is illegal, since qualifiers belong to the prototype alone.

    Common pitfall

    Including a class file into two scopes builds two same-named classes with no relationship. Handles, typedefs and statics silently fork, and the resulting type errors point nowhere near the duplicate include.

    Interview

    Likely question: a thousand-line driver class lands on your desk. How do you read it?

    • Read the 25-line extern prototype block first for the API.

    • Note which methods are protected extension hooks.

    • Open bodies only for the behavior under review.

    Check yourself An out-of-body method reads a local counter. Is that legal?

    Yes. The body is semantically inside the class, so local and protected members behave exactly as they do for in-body methods.

    Check yourself The same class file is included by two packages. What breaks?

    The type forks. Each inclusion declares its own class, so handles from one package never fit the other and statics double.

    Remember

    • extern puts the prototype in the class and the body outside it.

    • Qualifiers such as virtual and protected go on the prototype only.

    • Include each class file once, inside one package.

    Rule of thumb: the class body is the table of contents.

    How this was checked

    Run with Verilator 5.048 (--binary --timing) on 2026-10-18; every bench printed the PASS line shown, with no warnings. These class benches were run on Verilator only.

    Related topics

    • Next topic: Virtual Interfaces

    • Include vs import

    Previous

    48 / 259

    Next: Virtual Interfaces: the Class-to-Pins Bridge

    Up next

    Up nextVirtual Interfaces: the Class-to-Pins Bridge

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Static Properties & Methods
    • local, protected & Encapsulation
    • Parameterized Classes