Within a class, the order in which variables are defined determines the order in which they are initialized. Even though variable definitions are scattered between method definitions, they are still initialized before any method, including the constructor, is invoked.
Package javastatic;
Class window{
window (int marker) {
System.out.println ("Windows (" +marker+) ");
}
Class house{
window w1=new window (1);
House () {
System.out.println ("house ()");
W3=new Window (a);
}
Window W2=new window (2);
void F () {
System.out.println ("F ()");
}
Window W3=new window (3);
}
public class Orderofinitialization {
/**
* @param args
*
/public static void main (string[] args) { c22/>//TODO auto-generated method stubs House
house=new House ();
House.f ();
}
Results
Windows (1)
Windows (2)
Windows (3)
House () Windows (33)
F ()
This shows that the first initialization of the variable, followed by the construction method, in addition, W3 this reference was initialized two times.