A member declared as local is available only to the methods of the same class, and are not accessible by child classes. However, nonlocal methods that access local members can be inherited and overridden by child class.
Example
In the following example, we will declare two variables - one public and another local. We expect to see an error when a local member of the class is accessed from somewhere outside the class. This is because the keyword local is used to keep members local and visible only within the same class.
class ABC; // By default, all variables are public and for this example, // let's create two variables - one public and the other "local" byte public_var; localbyte local_var;
// This function simply prints these variable contents functionvoid display(); $display ("public_var=0x%0h, local_var=0x%0h", public_var, local_var); endfunction endclass
module tb; initialbegin
// Create a new class object, and call display method ABC abc = new(); abc.display();
// Public variables can be accessed via the class handle $display ("public_var = 0x%0h", abc.public_var);
// However, local variables cannot be accessed from outside $display ("local_var = 0x%0h", abc.local_var); end endmodule
As expected, the compiler gives out a compilation error pointing to the line where a local member is accessed from outside the class.
Simulation Log
1 2 3 4
$display ("local_var = 0x%0h", abc.local_var); | ncvlog: *E,CLSNLO (testbench.sv,24|47): Access tolocal member 'local_var' inclass 'ABC' isnot allowed here. irun: *E,VLGERR: An error occurred during parsing. Review thelogfilefor errors withthe code *E and fix those identified problems to proceed. Exiting with code (status 1).
In the above example, we can remove the line that causes a compilation error and see that we get a good output. The only other function that accesses the local member is the display() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
module tb; initialbegin
ABC abc = new();
// This should be able to print local members of class ABC // because display() is a member of ABC also abc.display();
// Public variables can always be accessed via the class handle $display ("public_var = 0x%0h", abc.public_var); end endmodule
Simulation Log
1 2 3 4
ncsim> run public_var=0x0, local_var=0x0 public_var = 0x0 ncsim: *W,RNQUIE: Simulation is complete.
In this example, let us try to access the local member from within a child class. We expect to see an error here also because local is not visible to child classes either.
// Define a base class and let the variable be "local" to this class class ABC; localbyte local_var; endclass
// Define another class that extends ABC and have a function that tries // to access the local variable in ABC class DEF extends ABC; function show(); $display ("local_var = 0x%0h", local_var); endfunction endclass
module tb; initialbegin
// Create a new object of the child class, and call the show method // This will give a compile time error because child classes cannot access // base class "local" variables and methods DEF def = new(); def.show();
end endmodule
As expected, child classes cannot access the local members of their parent class.
Simulation Log
1 2 3 4
$display ("local_var = 0x%0h", local_var); | ncvlog: *E,CLSNLO (testbench.sv,10|43): Access tolocal member 'local_var' inclass 'ABC' isnot allowed here. irun: *E,VLGERR: An error occurred during parsing. Review thelogfilefor errors withthe code *E and fix those identified problems to proceed. Exiting with code (status 1).