From 39143fdb29f62e237b92aca82dc8babf2700ea90 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 9 May 2023 13:00:34 -0600 Subject: [PATCH] Day 2 Final --- src/main/java/initialization/Example.java | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/initialization/Example.java diff --git a/src/main/java/initialization/Example.java b/src/main/java/initialization/Example.java new file mode 100644 index 0000000..73e46e3 --- /dev/null +++ b/src/main/java/initialization/Example.java @@ -0,0 +1,35 @@ +package initialization; + +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(x); } + 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!, 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! + +public class Example { +}