10. The relationship between code block, construction code block, static code block, and Main method

Source: Internet
Author: User

1, Ordinary code block :

The class capacity that appears in a method or statement between {} is called a normal code block, or a code block, for short. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code-"first occurrence first", i.e. sequential execution.

1 /*The first class below is legal, and the following two classes run in the same order, but generally no one writes it.*/2  Public classTest {3       {4System.out.println ("1"); 5       }6       intX=1;7System.out.println ("2"); 8       {9System.out.println ("3"); Ten       } One }  A  Public classTest1 { -  -System.out.println ("1");  the     intX=1; -System.out.println ("2");  -System.out.println ("3");  -}

2. Construct code block :

The simplest is to construct the code block in the method.

3. Static code block :

In general, if some code must be executed when the project is started, a static block of code is required, and the code is actively executed, and static blocks are used to initialize the class and initialize the properties of the class. each static block of code is executed only once .

1  PackageCom.st.common.tools;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.cfg.Configuration;6 7  Public classHibernatetools {8 9     Private StaticSessionfactory SF;//declaring a factory classTen/* This is a static block of code */ One     Static{ A         //creating a Configuration object for reading an XML file -Configuration conf =NewConfiguration (); -Conf.configure ("/hibernate.cfg.xml"); the          -         //Create a Sessionfactory factory class with Buildsessionfactory () -SF =conf.buildsessionfactory (); -     } +/* This is an ordinary block of code */ -      Public StaticSession opensession () { +         //get a session through Opensession () A         returnsf.opensession (); at     } -}

4, code block, construction code block, static code block and Main method at the time of execution of the priority level: (priority from high to low)

  Static code block >mian method > construct code block > Construct method.

  

10. The relationship between code block, construction code block, static code block, and Main method

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.