JavaIntermediate#puzzle#oop

What is the output of this code involving static and instance initialization order? class A { static { System.out.println("static A"); } { System.out.println("instance A"); } A() { System.out.println("constructor A"); } } new A(); new A();

Static blocks run once when the class is first loaded, before any instance is created. Instance blocks run every time a new object is created, right before the constructor body.

Example
new A(); new A();
// Output:
// static A
// instance A
// constructor A
// instance A
// constructor A

Related Questions

1
JavaIntermediate#puzzle#fundamentals

What does this print? Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d);

Open
2
JavaBeginner#puzzle#operators

What is the output? public class Test { public static void main(String[] args) { System.out.println(10 / 3); System.out.println(10.0 / 3); System.out.println(10 % 3); } }

Open
3
JavaAdvanced#puzzle#oop

What happens when you compile and run this code? class Parent { static void greet() { System.out.println("Parent"); } } class Child extends Parent { static void greet() { System.out.println("Child"); } } Parent p = new Child(); p.greet();

Open