I execute the following code and I wondered after seeing the result.
public class Example3 {
//section 01
static int a =checker();
//section 02
static{
System.out.println("inside the block");
}
//section 03
public static int checker(){
System.out.println("from checker");
return 20;
}
//section 04
public static void main(String[] args) {
System.out.println(a);
System.out.println("from main");
}
}
expected output
from checker
20
from main
real output
from checker
inside the block
20
from main
I divided the piece of code int 4 sections like section 01...etc to explain the problem.
when we start the java program we are starting from the main method. So in the main method has in below.
System.out.println(a); //step 01
System.out.println("from main"); //step 02
In the main method are two steps. I will explain in below that what I though.
Step 01
- go to the section 01.
- then move into the checker method(section 03)
- then print "from checker"
- then now "a" will takes the returned value of the checker method(a=20).
now step 01 finished.
Step 02
- will print "from main".
My problem is How additionally print "inside the block"?