diff --git a/Capture 9.png b/Capture 9.png new file mode 100644 index 0000000..f6aa23d Binary files /dev/null and b/Capture 9.png differ diff --git a/Questions.txt b/Questions.txt index d218ef0..4763a57 100644 --- a/Questions.txt +++ b/Questions.txt @@ -574,3 +574,34 @@ B) Exception at runtime C) Hello!, P-si: x is 0, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye! + +Q) Given: +class Parent { + Parent(int x) { y += x; out.print(", P-c: y is " + y); } + static { out.print(", P-si: x is " + Parent.x); } + static int x = 99; + int y = 100; + { y++; out.print(", P-i: y is " + y); } +} +class Child extends Parent { + int x = 200; + static int y = 300; + Child() { super(y); } + Child(int x) { this(); } + { out.print(", C-i x: is " + x); } + static { out.print(", C-si: y is " + y); } +} +class InitOrder3 { + public static void main(String[] args) { + out.print("Hello!"); + new Child(-1); + out.println(" Goodbye!"); + } +} + +What is the result? +A) Compilation fails +B) Exception at runtime +C) Hello!, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! +D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! +E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye! diff --git a/src/main/java/init2/Q.java b/src/main/java/init2/Q.java new file mode 100644 index 0000000..d1c21af --- /dev/null +++ b/src/main/java/init2/Q.java @@ -0,0 +1,28 @@ +package init2; + +import static java.lang.System.out; + +class Parent { + Parent(int x) { y += x; out.print(", P-c: y is " + y); } + static { out.print(", P-si: x is " + Parent.x); } + static int x = 99; + int y = 100; + { y++; out.print(", P-i: y is " + y); } +} +class Child extends Parent { + int x = 200; + static int y = 300; + Child() { super(y); } + Child(int x) { this(); } + { out.print(", C-i x: is " + x); } + static { out.print(", C-si: y is " + y); } +} +class InitOrder3 { + public static void main(String[] args) { + out.print("Hello!"); + new Child(-1); + out.println(" Goodbye!"); + } +} +public class Q { +} diff --git a/src/main/java/initialization/Example.java b/src/main/java/initialization/Example.java index 73e46e3..115e320 100644 --- a/src/main/java/initialization/Example.java +++ b/src/main/java/initialization/Example.java @@ -12,7 +12,8 @@ class Parent { class Child extends Parent { int x = 200; static int y = 300; - Child() { super(x); } +// Child() { super(x); } + Child() { super(100); } // avoid the compilation failure! Child(int x) { this(); } { out.print(", C-i x: is " + x); } static { out.print(", C-si: y is " + y); } diff --git a/src/main/java/initialization/LazyLoad.java b/src/main/java/initialization/LazyLoad.java new file mode 100644 index 0000000..ac2af0e --- /dev/null +++ b/src/main/java/initialization/LazyLoad.java @@ -0,0 +1,68 @@ +package initialization; + +class LazyP { + int p = getOneHundred(); + static { + System.out.println("loading LazyP"); + } + int getOneHundred() { + System.out.println("LazyP getOneHundred"); + return 100; + } + int getP() { + return p; + } +} +class Lazy extends LazyP { + { + System.out.println("running instance init in Lazy (1), x is " + this.x); + } + int x = 100; + { + System.out.println("running instance init in Lazy (2), x is " + x); + } + + + public Lazy() { + super(); + // right here, compiler adds "instance initialization" + System.out.println("back from super()"); + } + public Lazy(int x) { + this(); + // nothing special here... it all happened in and immediately after + // super() call above + } + static int getValue() { + System.out.println("getting value"); + return 10; + } + static { + System.out.println("loading Lazy, value is " + Lazy.value); + } + static int value = getValue(); + static { + System.out.println("loading Lazy (step 2), value is " + Lazy.value); + } + + int val = 200; + @Override + int getOneHundred() { + return val; + } +} + +public class LazyLoad { + public static void main(String[] args) { + System.out.println("step 1"); + Lazy l; + System.out.println("step 2"); + System.out.println("Class of Lazy is " + Lazy.class); + System.out.println("step 3a"); + System.out.println(Lazy.value); + System.out.println("step 3"); + l = new Lazy(); + System.out.println("step 4"); + System.out.println("p value is " + l.p); + } +}