fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<Integer> doesNotWork = new ArrayList<Integer>();
  13. doesNotWork.add(1);
  14. doesNotWork.add(2);
  15. doesNotWork.add(3);
  16. Test tst = new Test();
  17. //tst.setList(doesNotWork);
  18. Test.class.getMethod("setList", List.class).invoke(tst, doesNotWork);
  19. tst.showLengths(); // <<== However, this will now break.
  20. }
  21. }
  22.  
  23. class Test {
  24. private List<String> myList;
  25. public void setList(List<String> list) {
  26. myList = list;
  27. }
  28. public void showLengths() {
  29. System.out.println("Inside showLengths...");
  30. for (String s : myList) {
  31. System.out.println(s.length());
  32. }
  33. }
  34. }
  35.  
Runtime error #stdin #stdout #stderr 0.07s 380224KB
stdin
Standard input is empty
stdout
Inside showLengths...
stderr
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at Test.showLengths(Main.java:30)
	at Ideone.main(Main.java:19)