2015-09-07 11:44:36 -06:00
|
|
|
// housekeeping/OrderOfInitialization.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 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.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Demonstrates initialization order
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
*/
|