Understanding java-6.7 initialization and class loading from the beginning

Source: Internet
Author: User

In this chapter we will discuss the initialization and loading of classes.

Simply put, the class is loaded when the static member is accessed.

Let's take a more complex example and then explain the order of loading and initialization.

Package Com.ray.ch06;public class Sub extends Father {private int b = Print ("b");//5private static int c = Print ("C");// 1public Sub () {SYSTEM.OUT.PRINTLN ("Create sub"),//6}static {System.out.println ("sub Begin");//2}public static void Main (string[] args) {new Sub ();}} Class Father {private int a = print ("a");//3public static int print (String s) {System.out.println (s); System.out.println ("Father print"); return 0;} Public Father () {//4system.out.println ("Create Father");}}

Output:

C
Father Print
Sub Begin
A
Father Print
Create Father
B
Father Print
Create Sub


Execution order:

1. Execute the static part inside the Sub, including the domain and code block (Sub.class is loaded when the static property is accessed)

2. Execute the static part of the father, including domains and blocks of code

3. Execute the Father constructor

4. Execute sub Normal domain

5. Execute the Sub constructor


Note: In this case, the static part of the sub, the attribute domain and the code block are executed in the order of the Code, and there is no need to contact.


Summary: This chapter mainly discusses the loading and initialization sequence of classes.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-6.7 initialization and class loading from the beginning

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.