fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class X {
  8. static void bar(int x) {
  9. System.out.println("X::bar() with x = " + x);
  10. }
  11. }
  12. class Y extends X {
  13. static void bar() {
  14. System.out.println("Y::bar()");
  15. }
  16. }
  17.  
  18. /* Name of the class has to be "Main" only if the class is public. */
  19. class Ideone
  20. {
  21.  
  22.  
  23. public static void main (String[] args) throws java.lang.Exception
  24. {
  25. System.out.println("Static dispatch:");
  26. X.bar(1);
  27. Y.bar();
  28.  
  29. System.out.println("Dynamic dispatch of a static method (disallowed in Java):");
  30. Object[] arr = new Object[] { new X(), new Y() };
  31. for (Object a : arr) {
  32. a.bar(1);
  33. }
  34. }
  35. }
  36.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:32: error: cannot find symbol
      a.bar(1);
       ^
  symbol:   method bar(int)
  location: variable a of type Object
1 error
stdout
Standard output is empty