Can anyone suggest me whats the difference between Line 1 and LINE 2.Which is the preferred way?If we can create an object ,then why do we use static block? what is the order of execution of static members in a class?
class Foo {
static {
// bar = new Bar();
System.out.println("IN Block");//LINE 1
}
public static Bar bar = new Bar();//LINE 2
public void doStuff() {
System.out.println("I am in Foo");
}
}
Thanks