Java static code block, construction code block, and local code block
There are three types of code blocks to be learned, which have never been known before. Now we have sorted it out. As a note, I also hope to provide some help to the viewer.
Static code block
Class Demo
{
Static // static code block
{
}
}
It is executed as the class is loaded. And only run once (this is related to the long life cycle of the class ).
Purpose: Initialize the class.
Construct code blocks
Class Demo
{
{// Construct a code block
}
}
Independent code block in the class.
Purpose: Initialize all objects.
(The constructor initializes the corresponding object .)
Partial code block
Class Demo
{
Public void demo ()
{
{// Local code block
}
}
}
Empty code block in the method.
Purpose: restrict the lifecycle of a local variable.
If you have good suggestions, you can leave a message or send to the author's mailbox: fzb_xxzy@163.com