OnJava8-Examples/housekeeping/OrderOfInitialization.java

41 lines
1006 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// housekeeping/OrderOfInitialization.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Demonstrates initialization order.
// When the constructor is called to create a
// Window object, you'll see a message:
class Window {
2015-12-02 09:20:27 -08:00
Window(int marker) {
System.out.println("Window(" + marker + ")");
}
2015-06-15 17:47:35 -07:00
}
class House {
Window w1 = new Window(1); // Before constructor
House() {
// Show that we're in the constructor:
2015-11-03 12:00:44 -08:00
System.out.println("House()");
2015-06-15 17:47:35 -07:00
w3 = new Window(33); // Reinitialize w3
}
Window w2 = new Window(2); // After constructor
2015-11-03 12:00:44 -08:00
void f() { System.out.println("f()"); }
2015-06-15 17:47:35 -07:00
Window w3 = new Window(3); // At end
}
public class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f(); // Shows that construction is done
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Window(1)
Window(2)
Window(3)
House()
Window(33)
f()
2015-09-07 11:44:36 -06:00
*/