Java initialization order

Source: Internet
Author: User

1 .... If the class of the mian function contains static code blocks and static variables, the static code blocks and initial static variables are executed in sequence of static code blocks and variables, then, execute the common variable or common code block or method, and then execute the static main method. 2 ..... In the static main method, the code is executed in sequence. When initializing the object of the class where the main is located, the code is first executed in the initial order of common code blocks and variables, note: note that when only one object is generated, to execute common variables and common code blocks. public class Testmain {/*** @ param args **/public Testmain () {System. out. println ("This is a construct !!! ") ;}{ System. out. println (" 1111111111111111111 ") ;}static {System. out. println (" hello world !! ");} Public static void main (String [] args) {// TODO Auto-generated method stub {System. out. println (" I am before main !! ") ;}// New Fruit (); // ew Fruit (); new Testmain (); {System. out. println (" I am behinde main !! ") ;}} Static {System. out. println (" are you OK ??? ") ;}{ System. out. println (" 122222222222 ") ;}}: hello world !! Are you OK ??? I am before main !! 1111111111111111111122222222222 This is a construct !!! I am behinde main !! 3 .... When we want to create a class object, the initial order is .. Static variables or static code blocks or static methods in this class are initialized in sequence by code and then executed in sequence by common variables or common code blocks or common methods, they are also executed in the order of order in the Code. The execution I mentioned here is that the Loading Order of the content in a class does not necessarily execute the content in it, because some of them are loaded and not displayed for execution, we cannot display their order, but this is a mechanism in them, tested. Finally, the constructor in the execution is used to call the methods and attributes 4 in the object ,... We should note that the static code block and static variables are only initialized once, no matter how many objects, only when the first object is created, execute static tasks in sequence once ...... Class Fruit {private static int c = 99; {System. out. println (c);} static {System. out. println ("I is a static code ---- 2 ----- ***");} {System. out. println ("This is code of fruit !! -- 4 ") ;}int B = 88; {System. out. println (" This is code of fruit !! -- 3 "); System. out. println (B);} private int a; {System. out. println (" This is code of fruit !! -- 1 "); System. out. println (a);} public Fruit () {System. out. println (" it is a Fruit !! "); A = 3 ;}{ System. out. println (" This is code of fruit !! -- 2 "); System. out. println (a);} static {System. out. println ("I is a static code ---- 1 ----- ***") ;}} class Apple extends Fruit {int B; public Apple () {System. out. println ("I is apple, I is a fruit also !! ") ;}} Public class Testmain {/*** @ param args **/public Testmain () {System. out. println (" This is a construct !!! ") ;}{ System. out. println (" 1111111111111111111 ") ;}static {System. out. println (" hello world !! ");} Public static void main (String [] args) {// TODO Auto-generated method stub {System. out. println (" I am before main !! ") ;}// New Fruit (); // ew Fruit (); new Testmain (); {System. out. println (" I am behinde main !! ") ;}} Static {System. out. println (" are you OK ??? ") ;}{ System. out. println (" 122222222222 ") ;}} the class where the main function is located is different from the class that is no longer in the main function,

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.