Constraining across multiple objects

Hi,
I was trying to write a constraint for the following scenario:
Assume that class pkt has a random property ‘id’.
On calling randomize() for the 1st object of ‘pkt’ type it randomized to value of X
Then post this irrespective of the no. of objects created for ‘pkt’ type followed by call to randomize(),
id should retain it’s value of X
I tried two approaches ::
(1) Using static random property ( which I wasn’t sure was legal )

class pkt;
  
  static rand int id;
  
  function void post_randomize();
    id.rand_mode(0);
  endfunction    
endclass  

module top;
  
  pkt pkt1 , pkt2 , pkt3;
  
  initial begin
    pkt1 = new();    
    if(pkt1.randomize() ) $display("Success with %0p",pkt1);
    
    pkt2 = new();   
    if(pkt2.randomize() ) $display("Success with %0p",pkt2);
        
    pkt3 = new();    
    if(pkt3.randomize() ) $display("Success with %0p",pkt3);    
  end
endmodule  

However to my surprise it works !!

IEEE Std 1800-2023 Syntax 18-1 mentions

class_property ::= // from A.1.9
{ property_qualifier } data_declaration
| ...
property_qualifier10 ::=
random_qualifier
| class_item_qualifier
random_qualifier10 ::= rand | randc
10) In any one declaration, only one of protected or local is allowed, only one of rand or randc is allowed,
and static and/or virtual can appear only once.

(Q1) So this means one could club local , randc & static as well, correct ?

(Q2) Is there a pre-defined order of these qualifiers ? i.e protected/local is to be written first followed by rand/randc and then static OR they can be written in any order ?

(2) My 2nd approach ::

class pkt;
  
  static bit set;
  rand int id;
  
  constraint ID { if( set == 1 ) id == const'(id) ; }   
    
  function void post_randomize();
    set = 1;
  endfunction    
endclass
// Same module top declaration as above

I observe o/p such as ::
# Success with -1022841503 1
# Success with 0 1
# Success with 0 1

(Q3) Why does ‘id’ change during 2nd call to randomize but remains same during 3rd call ?

I expected the o/p to be
# Success with -1022841503 1
# Success with -1022841503 1
# Success with -1022841503 1

Thanks in Advance

A1) Yes, a class property can be local randc static
A2) They can be written in any order as long as they come before the data_declaration.

A3) id needs to be static as well to get your expected behavior.

Thanks Dave.
I realized that for (3) since default value of ‘id’ is 0, we observe id as 0 during 2nd & 3rd call to randomize()

I tried this with a different approach. Though, I feel your approach is much better:

class abc;
  rand bit[7:0] id;
  static bit[7:0] val, count;
  
  constraint abc_c{
    if(count >0) 
      id == val;
  }
  function void post_randomize();
    if(count == 0) val = id;
    count++;
    $display("id =%0p count =%0p val =%0p", id, count, val);
  endfunction
  
endclass

module tb;
  abc abc_inst;
  
  initial begin 
    abc_inst = new();
    repeat(10)
      abc_inst.randomize();
  end
endmodule