Skip to content

Commit

Permalink
Day 3 break 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 10, 2023
1 parent 39143fd commit 995e08b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
Binary file added Capture 9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
28 changes: 28 additions & 0 deletions src/main/java/init2/Q.java
Original file line number Diff line number Diff line change
@@ -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 {
}
3 changes: 2 additions & 1 deletion src/main/java/initialization/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/initialization/LazyLoad.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 995e08b

Please sign in to comment.