1, Common code block
Package com.java1234.chap03.sec07; Public class Demo1 { publicstaticvoid main (string[] args) { int a=1; /** Normal code block (sequential execution) * */ { a=2; System.out.println ("Normal code block"); } System.out.println ("a=" +a);} }
2, construction block
Package com.java1234.chap03.sec07;
public class Demo2 {
/**
* Building Blocks (bridging the gaps in constructors)
*/
{
SYSTEM.OUT.PRINTLN ("General construction Method");
}
Public Demo2 () {
System.out.println ("Construction Method One");
}
Public Demo2 (int i) {
System.out.println ("Construction Method II");
}
Public Demo2 (int i,int j) {
System.out.println ("Construction Method three");
}
public static void Main (string[] args) {
New Demo2 ();//instantiation of an Object anonymous class
}
}
3, Static code block
Package com.java1234.chap03.sec07;
public class Demo3 {
/** Building Blocks
*
*/
{
SYSTEM.OUT.PRINTLN ("Common building Block");
}
/* Static code block (only once, factory executes only once)
* */
static{
System.out.println ("Static code block");
}
Public Demo3 () {
System.out.println ("Construction Method One");
}
Public Demo3 (int i) {
System.out.println ("Construction Method II");
}
Public Demo3 (int i,int j) {
System.out.println ("Construction Method three");
}
public static void Main (string[] args) {
New Demo3 ();//instantiation of an Object anonymous class
New Demo3 (2);
New Demo3 (1, 2);
}
}
1, normal code block 2, building block 3, static code block