Class properties can be
made read-only by a const
declaration like any other SystemVerilog variable. However, because class objects are dynamic
objects, class properties allow two forms of read-only variables: Global
constants and Instance constants.
Global constant
properties are those that include an initial value as part of their
declaration. They are similar to other const variables in that they cannot be
assigned a value anywhere other than in the declaration.
class Jumbo_Packet;
const int max_size = 9 * 1024; // global constant
byte payload [*];
function
new( int size );
payload = new[ size > max_size
? max_size : size ];
endfunction
endclass
Instance constants do
not include an initial value in their declaration, only the const qualifier. This type of constant can be assigned a value
at run-time, but the assignment can only be done once in the corresponding
class constructor.
class Big_Packet;
const int size; //
instance constant
byte payload [*];
function
new();
size = $random %
4096; //one assignment in
new -> ok
payload = new[ size ];
endfunction
endclass
Typically, global constants
are also declared static since they
are the same for all instances of the class.
However, an instance constant cannot be declared static, since that would disallow all assignments in the
constructor.