fork download
  1. class C<S> {
  2. private S testField;
  3.  
  4. public C(S testField) {
  5. this.testField = testField;
  6. }
  7.  
  8. public S getTestField() {
  9. return testField;
  10. }
  11. }
  12.  
  13. class Test {
  14. public static void main(String[] args) {
  15. C rawArray[] = new C[2];
  16. rawArray[0] = new C<String>("oh hi Mark");
  17. rawArray[1] = new C<Integer>(123123);
  18.  
  19. C<String> strArray[] = rawArray;
  20. C<String> s1 = strArray[0];
  21. C<String> s2 = strArray[1];
  22. System.out.println(s1.getTestField());
  23. System.out.println(s2.getTestField());
  24. }
  25. }
Runtime error #stdin #stdout #stderr 0.04s 2184192KB
stdin
Standard input is empty
stdout
oh hi Mark
stderr
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at Test.main(Main.java:23)