fork(1) download
  1. class Example
  2. {
  3. public static void main (String[] args) throws java.lang.Exception
  4. {
  5. new C().test();
  6. }
  7. }
  8.  
  9. class A {
  10. public int x = 1;
  11. }
  12.  
  13. class B extends A {
  14. public int x = 2;
  15. }
  16.  
  17. class C extends B {
  18. public int x = 3;
  19.  
  20. public void test() {
  21. //There are two ways to put x in C from the method test():
  22. System.out.println("(Before) A.x = " + ((A)this).x);
  23. System.out.println("(Before) B.x = " + ((B)this).x);
  24. System.out.println("(Before) C.x = " + this.x);
  25. ((A)this).x = 4;
  26. System.out.println("(After) A.x = " + ((A)this).x);
  27. System.out.println("(After) B.x = " + ((B)this).x);
  28. System.out.println("(After) C.x = " + this.x);
  29. }
  30. }
  31.  
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
(Before) A.x = 1
(Before) B.x = 2
(Before) C.x = 3
(After) A.x = 4
(After) B.x = 2
(After) C.x = 3