You have a number of problems with your code, the biggest one is you forgot to declare size as rand.
class Object;
rand int id;
endclass
class Objects;
rand Object lst[];
rand int size; // you forgot to declare rand
constraint default_size {
size > 0 && size < 6;
}
constraint default_lst {
lst.size() == size;
}
// A better way to write these constraints without size
constraint size_c { lst.size() inside {[1:5]};}
function void post_randomize();
lst = new[size];
foreach (lst[i]) begin
lst[i] = new; // need to construct each element
lst[i].id = i;
end
endfunction
endclass
module test1; // don't use program blocks
Objects objs; // declare static variables outside of initial blocks
initial begin
objs = new;
void'(objs.randomize()); // normally should check the result
$display("Allocated an array of size=%0d", objs.lst.size());
end
endmodule : test1