fork(1) download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. BarManager bm = new BarManager();
  9. System.out.println(bm.get());
  10. }
  11. }
  12.  
  13. class BarManager {
  14. private List<Integer> bars;
  15.  
  16. BarManager() {
  17. // build using a mutable list, make an immutable copy when done
  18. ArrayList<Integer> bars = new ArrayList<>();
  19. bars.add(1);
  20. bars.add(2);
  21. bars.add(3);
  22. this.bars = List.copyOf(bars);
  23. }
  24.  
  25. public List<Number> get() {
  26. return List.copyOf(bars); // does not actually copy
  27. }
  28. }
  29.  
Success #stdin #stdout 0.08s 32460KB
stdin
Standard input is empty
stdout
[1, 2, 3]