Static variable in parametric class

class A #(int a = 10);
static int b = a;
static int c ;
endclass
module main;
A #(30) a1;
A #(50) a2;
initial begin
a1 = new();
a2 = new();
$display (“a1.b =%0d,\t a2.b=%0d,\t a1.c= %0d,\t a2.c = %0d”,a1.b,a2.b,a1.c,a2.c);
a1.b = 111;
a1.c = 121;
$display (“a1.b =%0d,\t a2.b=%0d,\t a1.c= %0d,\t a2.c = %0d”,a1.b,a2.b,a1.c,a2.c);
end
endmodule

the value of b and c are different in both the objects, even they are defined as static?

class A #(int a = 10);
static int b = a;
static int c ;
endclass
module main;
A #(30) a1;
A #(50) a2;
initial begin
a1 = new();
a2 = new();
if(!$cast(a1,a2))
  $display("Error");
$display ("a1.b =%0d,\t a2.b=%0d,\t a1.c= %0d,\t a2.c = %0d",a1.b,a2.b,a1.c,a2.c);
a1.b = 111;
a1.c = 121;
$display ("a1.b =%0d,\t a2.b=%0d,\t a1.c= %0d,\t a2.c = %0d",a1.b,a2.b,a1.c,a2.c);
end
endmodule

Output:

Error

a1.b =30, a2.b=50, a1.c= 0, a2.c = 0

a1.b =111, a2.b=50, a1.c= 121, a2.c = 0

It means assignment to object of class A #( 30) from object of class A #( 50) is illegal which says both are different classes. And Static works for objects of single(same) class.

Thanks,
Rajashekhar.

In reply to kothaluri rajashekhar:

since parameters are different values, it means classes are of different type.is it ??

In reply to VPS:

Yes,

If we have

A #(30) a1;
A #(30) a2;            //A #(50) a2;

Then both classes are treated as one and the static keyword works.

Thanks,
Rajashekhar.

If you want parametrized classes with different specializations to share common static variables, they the class should be extended from a common unparametrized base class. See my DVCon paper Using Parameterized Classes and Factories: The Yin and Yang of Object-Oriented Verification which has a good explanation of how static variables work with classes.