In my understanding of static members of a class it will be executed depending on the sequence they were written on the class for example:
class GelInk{
static{
System.out.println("First Static");
}
{
System.out.println("Initialization block don't mind me");
}
static{
System.out.println("Second Static");
}
}
//Results to
First Static
Second Static
Initialization block don't mind me
but when I tried this code:
class Ink{
static{
a=printStaticMessages(0);
}
static int a = printStaticMessages(1);
Ink(){
System.out.println(a);
}
static int printStaticMessages(int a){
System.out.println("This is a static messages method " + a);
return a;
}
}
it compiles fine, I expected a compilation error or something because the static variable a is being used by the static block before it was being declared. The above code results to the following when the object of the class was created:
This is a static messages method 0
This is a static messages method 1
1