Precedence Level in object oriented Programming(not Operator Precedence)
In which order is the precedence of the following Constructor, Static Bl开发者_JAVA技巧ock and Non Static Block Thanks
A non static block will execute when the class is initialized. A static block will only be executed once.
The constructor will execute when the object is instantiated.
A static block will execute when the object is instantiated.
That will depend on your language.
For Java, the static block will always be executed first, followed by the non static block and then by the constructor.
public class Q20 {
static int i;
int j;
static {
System.out.println("static block");
}
{
System.out.println("non static block");
}
public Q20() {
System.out.println("constructor");
}
public static void main(String args[]) {
Q20 q = new Q20();
}
}
static block
non static block
constructor
精彩评论