Though this is a small question to ask, wanted to understand this strange behavior! Following is the code and the behavior of the code that's in discussion (through the console output).
public class EmptyBracesWithinClass {
public static void main(String[] args) {
EmptyBraces eb = new EmptyBraces();
System.out.println("SYSO within main() method");
}
}
class EmptyBraces {
{
System.out.println("SYSO within empty braces");
}
public EmptyBraces() {
System.out.println("SYSO within constructor() method");
}
}
Console output:
SYSO within empty braces
SYSO within constructor() method
SYSO within main() method
The question here is, why would the piece of code within the empty braces get executed first during the object instance creation of EmptyBraces
class (though it is never declared as STATIC
explicitly)?