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