"Java" Static code block

Source: Internet
Author: User

Static code block

    • Format:

      Static

      {

      Execution statements in a static code block

      }

    • Characteristics:

      Executes as the class loads, executes only once, initializes the class, and takes precedence over the main function execution

      Consider the following Java statement

Class staticcode{static {System.out.println ("a");    }}public class Demo {static {System.out.println ("B");        } public static void Main (string[] args) {new Staticcode ();        New Staticcode ();    System.out.println ("End");    } static {System.out.println ("C"); }  }

The output is:

B

C

A <== Although there are two statements that call Staticcode, but because they contain only static blocks of code, static blocks only perform

Once, so only one A is output

End


Classes are loaded only when the content in the class is involved, and static blocks of code execute

Staticcode m = null; The <== class is not loaded

s = new Staticcode (); The <== class was loaded because the constructor Staticcode () in the class was used, so the static code block

will be executed

So judging if a class has not been loaded, you can use static code blocks to verify

Class staticcode{    staticcode ()     {         system.out.println ("C");    }    static     {        system.out.println ("a");             I    }         {        system.out.println ("B");             II    }         staticcode (int x)     {         system.out.println ("hah");          iii     }        public static void test ()     {         system.out.println ("succeed");     }}public  class Demo {        public static void  Main (String[] args)     {        new  Staticcode (5);     }      }

I-----A static block of code to initialize the class

II-----Constructing a code block to initialize the object

III-----constructor to initialize the corresponding object

Output Result:

A

B

Hah


Note: Static code blocks can only access static variables

Constructs a block of code that can be accessed to

"Java" 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.