Java Program Execution Order

Source: Internet
Author: User

give the conclusion first:Execution Order: Parent class static Block--subclass static block (before main function execution), parent class block, parent class constructor, child class constructor, subclass blockAssignment Order: Member variable assignment, block assignment, and constructor assignment
1 classa{2     Static{System.out.println ("Class A");}3{System.out.println ("Class C");}4      PublicA () {5System.out.println ("This class A");6     }7 }8 9  Public classTest1extendsa{Ten     Static{System.out.println ("Class B");} One      PublicTest1 () { ASystem.out.println ("This class B"); -     } -{SYSTEM.OUT.PRINTLN ("Class D");} the      Public Static voidMain (string[] args) { -SYSTEM.OUT.PRINTLN ("----Start---"); -         NewTest1 (); -         NewTest1 (); +SYSTEM.OUT.PRINTLN ("---end---"); -     } + } A  at The result is: - Class A - classB -----Start--- - Class C -  This classA in classD -  This classB to Class C +  This classA - classD the  This classB *---end---
View Code

****

The assignment step is

    1. Static variable assignment for parent class
    2. Static variable assignment of its own value
    3. Parent class Member variable assignment
    4. Parent Class block Assignment
    5. Parent class constructor Assignment value
    6. Assigning values to self-member variables
    7. Self-block assignment
    8. Self constructor Assignment

Java program Execution order

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.