class Details {
static private int age;
static {
age=45;
System.out.println("Static block.");
}
private String name;
{
name="Satyam";
System.out.println("Initialiser block");
}
Details(){
System.out.println("constructor.");
}
static void staticMethod(){
System.out.println("static method.");
}
void nonStaticMethod(){
System.out.println("non-static method");
}
}
public class EncapsulationPractice {
public static void main(String[] args) {
System.out.println("main method");
Details.staticMethod();
Details details=new Details();
details.nonStaticMethod();
}
}
I'm getting output in this way :
main method
Static block.
static method.
Initialiser block
constructor.
non-static method
But as far as the blogs I read and vedios I watched they told that static variables and static blocks are initialized before the obj creation i.e at the time of class loading? I'm confused between class loading and initialization and I was expecting the output of above code as following:
Static block.
main method
static method.
Initialiser block
constructor.
non-static method