Your code “works” because the method class_A::compare makes no references to any non-static class properties. Most implementations add an implicit this handle argument to non-static class methods, and it would not be until the method tries to reference the null this handle that you would see an error. However, the LRM does not guarantee this behavior and you should declare compare() as a static method so the compiler will check that compare does not access any non-static class properties.
Accessing non-static members (see 8.9) or virtual methods (see 8.20) via a null object handle is illegal. The result of an illegal access via a null object is indeterminate, and implementations may issue an error.
module
class myclass
int A=1;
function void print;
$display(A);
endfunction
endclass
myclass h;
initial begin
h = new;
h.print;
end
endmodule
The above code is usually implemented as
module
class myclass
int A=1;
endclass
function void my_class_print(myclass this);
$display(this.A);
endfunction
myclass h;
initial begin
h = new;
myclass_print(h);
end
endmodule