Hello, during my studies in Java I came across the following doubt
Given the code below:
class Foo extends Goo {
static {
System.out.println("1");
}
{
System.out.println("2");
}
public Foo() {
System.out.println("3");
}
public static void main(String[] args) {
System.out.println("4");
Foo f = new Foo();
}
}
class Goo {
static {
System.out.println("5");
}
{
System.out.println("6");
}
Goo() {
System.out.println("7");
}
}
I get the following output:
5
1
4
6
7
2
3
The output leads us to infer the following order of execution: static blocks and non-static blocks just before the constructs. This question has clarified a lot for me about static blocks. What is not clear is the order in which the classes are loaded by the classloader, I thought that the class Foo
would be loaded before because it is declared before, but the output tells me that this is not how it works. Does anyone know what the classloader rule is?