fork download
  1. import java.util.Random;
  2.  
  3. class Ideone {
  4. public static void main (String[] args) {
  5. Data a = new Data(0),
  6. b = new Data(0),
  7. c = new Data(0);
  8. Data[] abc = {a, b, c}; //copied reference as value
  9. Random rand = new Random();
  10. for(Data d : abc) {
  11. d.i = rand.nextInt(10); //updating value at reference
  12. System.out.printf("d = %s\n", d);
  13. }
  14. System.out.printf("a = %s, b = %s, c = %s\n", a,b,c);//updated
  15. }
  16. }
  17.  
  18. class Data {
  19. int i;
  20. public Data(int i) {
  21. this.i = i;
  22. }
  23. @Override
  24. public String toString() {
  25. return Integer.toString(i);
  26. }
  27. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
d = 9
d = 3
d = 7
a = 9, b = 3, c = 7