Java Basics-Object Initialization __java

Source: Internet
Author: User
1. Java objects are initialized in the following order:
Parent class StaticFields and StaticBlock-> Subclass StaticFields and StaticBlock
-> Parent class Non- StaticFields and non- StaticBlock-> Parent Class construction method
-> Sub Class non- StaticFields and non- StaticThe construction method of block-> sub-class
2. The sample code is as follows:
Package Org.demo;
    public class Initdemo {public static void main (string[] args) {new Child ("Init");
    Class Parent {private static int total = 1;
    
    private int id = print ("id");
    private static Parent P1 = new Child ("Child1");
    private static Parent P2 = new Child ("child2");
    private static int i = print ("i");
    private static int j = 99;
    static {print ("static block");
    } {print ("building Block");
        Public Parent (String str) {System.out.println ("Parent," + (total++) + ":" + str + "i=" + i + "j=" + j);
        ++i;
        ++j;
    System.out.println (ToString ());  public static int print (String str) {System.out.println ("parent," + (total++) + ":" + str + "i=" + I
        + "j=" + j);
        ++j;
    return ++i;
    Public String toString () {return ' parent, total= ' + total + ' id= ' + ID;
    Class Child extends Parent {private static int total =-1; Private int id =-1;
        Public Child (String name) {super (name);
        if (Total < 0) {total = 1;
        else {total++;
        } id=total;
    System.out.println (ToString ());
        public static int print (String str) {System.out.println ("child, str=" + str);
    return 0;
    Public String toString () {return ' child, total= ' + total + ' id= ' + ID; }/* Parent, 1:id i=0 j=0 Parent, 2: Construction block I=1 j=1 parent, 3:child1 i=2 j=2 Child, total=0 id=0 Child, Total=1 id=1 T, 4:id i=3 j=3 Parent, 5: Tectonic block i=4 j=4 parent, 6:child2 i=5 j=5 Child, total=1 id=0 Child, total=2 id=2 Parent, 7:i I=6 J 
 =6 Parent, 8: static block i=7 j=99 parent, 9:id i=8 j=100 Parent, 10: Tectonic block i=9 j=101 parent, 11:init i=10 j=102 Child, Total=-1 id=0
 Child, total=1 id=1 * *
3. The analysis is as follows:

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.