Go Static Block __java in Java

Source: Internet
Author: User
static block in Java

From: Javaeye Author: mht19840918 keywords: static

A structure such as static {} is often used in Java programs.
Such a structure is called a static block, typically a code that runs when a class is initialized. Note that the initialization of the class is not the initialization of the object.
That is, you define the object is the initialization code that it runs, and it runs only the first time it is defined. After that, the initialization is no longer running.
There can be a lot of static blocks in a class. Static blocks are executed sequentially.

Look at the following code: public class teststatic{
static{System.out.println (1);  } static{System.out.println (2);  public static void Main (string[] args) {System.out.println (5);  } static{System.out.println (3);  } static{System.out.println (4); The result is 1 2 3 4 5 at a glance, first calling static static fast code, and then invoking the Startup method. The execution order of the static blocks is the process of executing the code in the order in which it is written. Static blocks are used less, in fact, when the object is generated to produce a series of static variables.

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.