Load order of various members in a Java class

Source: Internet
Author: User

Source:

[Java]View PlainCopy 
  1. Public class Superandsub {
  2. public static void Main (string[] args) {
  3. //Super S1 = new Sub ();
  4. //Super S2 = new super ();
  5. Sub S3 = new Sub ();
  6. }
  7. }
  8. Class Super {
  9. static int a = Geta ();
  10. Static {
  11. System.out.println ("Loading super static Block");
  12. }
  13. int b = GETB ();
  14. {
  15. System.out.println ("load the ordinary block of Super");
  16. }
  17. Super () {
  18. System.out.println ("load Super constructor");
  19. }
  20. static int Geta () {
  21. System.out.println ("Load super static variable");
  22. return 1;
  23. }
  24. static int Getb () {
  25. System.out.println ("load instance variable of Super");
  26. return 2;
  27. }
  28. }
  29. Class Sub extends Super {
  30. static int c = GetC ();
  31. Static {
  32. System.out.println ("Static block for loading sub");
  33. }
  34. int d = getd ();
  35. {
  36. System.out.println ("Normal block for loading sub");
  37. }
  38. Sub () {
  39. System.out.println ("constructor to load sub");
  40. }
  41. static int GetC () {
  42. System.out.println ("Static variable for load sub");
  43. return 1;
  44. }
  45. static int getd () {
  46. System.out.println ("Load instance variable of sub");
  47. return 2;
  48. }
  49. }



Output:

[HTML]View PlainCopy  
      1. Load Static variables of super
      2. Loading a static block of super
      3. Load static variable of sub
      4. Loading a static block of a sub
      5. To load an instance variable of super
      6. Load a super normal block
      7. To load the Super constructor
      8. To load an instance variable of a sub
      9. Load the normal block of a sub
      10. The constructor that loads the sub

Load order of various members in a Java class

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.