Initialization order of objects in Java

Source: Internet
Author: User

Class Helloa {
Public Helloa () {
System.out.println ("Helloa");
}
{System.out.println ("I ' M A class");}
static {System.out.println ("static A");}
}
public class Hellob extends Helloa {
Public Hellob () {
System.out.println ("Hellob");
}
{System.out.println ("I ' M B class");}
static {System.out.println ("static B");}
public static void Main (string[] args) {
System.out.println ("Main () Start");
New Hellob ();
System.out.println ("Main () end");
}
}

The result of the operation is:

Static A
Static B
Main () Start
I ' M A class
Helloa
I ' M B class
Hellob
Main () End

Object Initialization Order: (1) After the class is loaded, the static modified statement is executed from top to bottom (from parent to subclass), (2) when the static statement is executed, then the main method is executed, and (3) If the statement new has its own object, the construction code block is executed from top to bottom, Constructors (both of which can be said to be bound together).

Initialization order of objects in Java

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.