/* test various tricky aspects of constructors */ public class mainer { public static void main(String[]args) { System.out.println("Test chaining 1..4"); C x = new C(1,2,3); System.out.println("checking"); x.check(1,2,3); System.out.println("testing dynamic dispatch foo"); x.doit(); System.out.println("Test immutable default constructor chaining 1..2"); I i1; System.out.println("checking"); i1.check(10,20,30); System.out.println("Test immutable constructor chaining 1..3"); I i2 = new I(1,2,3); System.out.println("checking"); i2.check(1,2,3); System.out.println("done."); } } class A { public int x = 10; public int a = 0; public inline A(int a) { this.a = a; System.out.println("ok1"); } public inline void foo() { System.out.println("okfoo"); } } class B extends A { public int y = 20; public int b = 0; public int d = 101; public int e = 102; public static int f = 103; public static int g; public int h = (d + e + f + g ) * 2; // =612 public int i = returns666(); public static int j = returns666()+f; // =769 public static inline int returns666() { return 666; } public inline B(int a, int b) { super(a); this.b = b; System.out.println("ok2"); } public void bogus1() { System.out.println("wrong method!!!"); } public void bogus2() { System.out.println("wrong method!!!"); } } class C extends B { public int z = 30; public int c = 0; public byte[] k = new byte[10]; public static byte[] l = new byte[10]; public inline C(int a, int b, int c) { this(a,b,c,0); System.out.println("ok4"); } private inline C(int a, int b, int c, int junk) { super(a,b); this.c = c; System.out.println("ok3"); } public inline void foo() { System.out.println("wrong method!!!"); } public inline void doit() { super.foo(); } public inline void check(int a, int b, int c) { if (x != 10) System.out.println("wrong x"); if (y != 20) System.out.println("wrong y"); if (z != 30) System.out.println("wrong z"); if (this.a != a) System.out.println("wrong a"); if (this.b != b) System.out.println("wrong b"); if (this.c != c) System.out.println("wrong c"); if (this.d != 101) System.out.println("wrong d"); if (e != 102) System.out.println("wrong e"); if (f != 103) System.out.println("wrong f"); if (g != 0) System.out.println("wrong g"); if (h != 612) System.out.println("wrong h"); if (i != 666) System.out.println("wrong i"); if (j != 769) System.out.println("wrong j"); if (k == null || k.length != 10) System.out.println("wrong k"); if (l == null || l.length != 10) System.out.println("wrong l"); } } immutable class I { public int a; public int b; public int c; // bug here in original compiler public int d = 101; public int e = 102; public static int f = 103; public static int g; public int h = (d + e + f + g ) * 2; // =612 public int i = returns666(); public static int j = returns666()+f; // =769 public static inline int returns666() { return 666; } public inline I(double f) { System.out.println("okdI1"); a = 10; b = 20; c = 30; } public inline I() { this(1.1); System.out.println("okdI2"); } public inline I(int a, int b, int c) { this(a,b); System.out.println("okI3"); this.c = c; } public inline I(int a, int b) { this(a); System.out.println("okI2"); this.b = b; } public inline I(int a) { this.a = a; System.out.println("okI1"); } public inline void check(int a, int b, int c) { if (this.a != a) System.out.println("wrong a"); if (this.b != b) System.out.println("wrong b"); if (this.c != c) System.out.println("wrong c"); if (this.d != 101) System.out.println("wrong d"); if (e != 102) System.out.println("wrong e"); if (f != 103) System.out.println("wrong f"); if (g != 0) System.out.println("wrong g"); if (h != 612) System.out.println("wrong h"); if (i != 666) System.out.println("wrong i"); if (j != 769) System.out.println("wrong j"); } }