Java class loading mechanism (record only)

Source: Internet
Author: User

The following declares the class to see the test results:

[Java]View PlainCopy
  1. Package Com.shareniu;
  2. Public class Helloa {
  3. {
  4. System.out.println ("before class Helloa");
  5. }
  6. Public Helloa () {
  7. System.out.println ("Helloa");
  8. }
  9. static{
  10. System.out.println ("static Helloa");
  11. }
  12. {
  13. System.out.println ("after class Helloa");
  14. }
  15. }
  16. Public class Hellob extends Helloa {
  17. private int i;
  18. Public Hellob () {
  19. System.out.println ("Hellob");
  20. }
  21. {
  22. System.out.println (i);
  23. System.out.println ("before class Hellob");
  24. }
  25. {
  26. System.out.println ("after class Hellob");
  27. }
  28. static{
  29. System.out.println ("static Hellob");
  30. }
  31. Public static void Main (string[] args) {
  32. New Hellob ();
  33. System.out.println ("Shareniu");
  34. New Hellob ();
  35. }

What is the result of the output?

[Java]View PlainCopy
  1. Static Helloa
  2. Static Hellob
  3. Before class Helloa
  4. After class Helloa
  5. Helloa
  6. 0
  7. Before class Hellob
  8. After class Hellob
  9. Hellob
  10. Shareniu
  11. Before class Helloa
  12. After class Helloa
  13. Helloa
  14. 0
  15. Before class Hellob
  16. After class Hellob
  17. Hellob

1.1.1.Summary

1. When instantiating a class object, the static method of the parent class is instantiated first, and then the static method of the subclass is instantiated and executed only once.

2. The code of the static block has the highest priority.

3. Initialize the code block of the parent class before executing the parent class's construction method. Continue to initialize your own block of code, and then execute your own construction method.

4. Code blocks are executed in favor of the construction method. This is the result of the 2-time construction of class object initialization. The code block initializes all of the variable information, and when the constructor is executed, the memory of the modified value is initialized again if there is a variable assignment.

5. Always load the parent class first, and the child class is loaded. Load the static variable information in the Loaded class object first.

6. The execution order of the code block {} is executed from top to bottom.

Java class loading mechanism (record only)

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.