I\'m looking at the code below and found something a bit strange:
public class Sequence {
Sequence() {
System.out.print(\"c \");
}
{
static {
System.out.print("x ");
}
Static blocks
are only executed once when the class is loaded and initialized by the JRE.
And non-static
block will be call every time your are creating a new instance and it will be call just before the Constructor.
As here you've created only 1 instance of Sequence
so constructed has been called after non-static
blocks and then the method which actually your goal.
That's an instance initialization block followed by a static initialization block.
{
System.out.print("y ");
}
gets called when you create an instance of the class.
static {
System.out.print("x ");
}
gets called when the class is loaded by the class loader. So when you do
new Sequence().go();
the class gets loaded, so it executes static {}
, then it executes the instance initialization block {}
, afterwards the body of the constructor is called, and then the method on the newly created instance. Ergo the output x y c g
.
{
System.out.print("y ");
}
These kinds of blocks are called initializer block
. It is executed every time you create an instance of a class
.
At compile time, this code is moved into every constructor of your class.
Where as in case of static initializer
block: -
static {
System.out.println("x ");
}
it is executed once when the class is loaded.
We generally use static
initializer block when the initialization of a static
field, require multiple steps.
It is used as an initialisation block and runs after any static declaration. It could be used to ensure that no one else can create an instance of the class (In the same way you would use a private constructor) as with the Singleton design pattern.
static {
System.out.print("x ");
}
is an initialization block shared by the class(as indicated by static
), which is executed first.
{
System.out.print("y ");
}
is an initialization block shared by all objects(constructors) of the class, which comes next.
Sequence() {
System.out.print("c ");
}
is a particular constructor for the class, which is executed third. The instance initialization block is invoked first every time the constructor is executed. That's why "y" comes just before "c".
void go() {
System.out.print("g ");
}
is just an instance method associated with objects constructed using the constructor above, which comes last.
Its not a method but a initialization block.
{
System.out.print("y ");
}
It will be executed before the constructor call. While
static {
System.out.print("x ");
}
is static initialization block which is executed when class is loaded by class loader.
So when you run your code
1. Class is loaded by class loader so static initialization block is executed
Output: x is printed
2. Object is created so initialization block is executed and then constuctor is called
Output: y is printed followed by c
3. Main method is invoked which in turn invokes go method
Output: g is printed
Final output: x y c g
This might help http://blog.sanaulla.info/2008/06/30/initialization-blocks-in-java/