1, normal code block 2, building block 3, static code block

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.