Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.concurrent.TimeUnit;
  2.  
  3. public class Solution {
  4.     public static void main(String[] args) {
  5.         new Child();
  6.     }
  7. }
  8.  
  9. class Parent {
  10.     private static final String staticName = initName();
  11.  
  12.     private final String instanceAccount = initAccount();
  13.  
  14.     static {
  15.         //static block #1
  16.         System.err.println("4. Initializing: static block #1 (Parent)");
  17.         sleep();
  18.     }
  19.  
  20.     private static final int staticAge = initAge();
  21.  
  22.     static {
  23.         //static block #2
  24.         System.err.println("6. Initializing: static block #2 (Parent)");
  25.         sleep();
  26.     }
  27.  
  28.     {
  29.         //instance initialize block #1
  30.         System.err.println("12. Initializing: instance block #1 (Parent)");
  31.         sleep();
  32.     }
  33.  
  34.     private final int instanceTotal = initTotal();
  35.  
  36.     {
  37.         //instance initialize block #2
  38.         System.err.println("14. Initializing: instance block #2 (Parent)");
  39.         sleep();
  40.     }
  41.  
  42.     public Parent() {
  43.         System.err.println("15. Executing body of Parent constructor (Parent)");
  44.         sleep();
  45.     }
  46.  
  47.     private String initAccount() {
  48.         System.err.println("11. Initializing: instance variable - instanceAccount (Parent)");
  49.         sleep();
  50.         return "Angela";
  51.     }
  52.  
  53.     private int initTotal() {
  54.         System.err.println("13. Initializing: instance variable - instanceTotal (Parent)");
  55.         sleep();
  56.         return 100;
  57.     }
  58.  
  59.     private static String initName() {
  60.         System.err.println("3. Initializing: static - name (Parent)");
  61.         sleep();
  62.         return "Angela";
  63.     }
  64.  
  65.     private static int initAge() {
  66.         System.err.println("5. Initializing: static - age (Parent)");
  67.         sleep();
  68.         return 25;
  69.     }
  70.  
  71.     protected static void sleep() {
  72.         try {
  73.             TimeUnit.SECONDS.sleep(1);
  74.         } catch (InterruptedException e) {
  75.         }
  76.     }
  77. }
  78.  
  79. class Child extends Parent {
  80.     private static final String address = initAddress();
  81.  
  82.     private final String instanceKey = initKey();
  83.  
  84.     static {
  85.         //static block #3
  86.         System.out.println("8. Initializing: static block #3 (Child)");
  87.         sleep();
  88.     }
  89.  
  90.     private static final int count = initCount();
  91.  
  92.     static {
  93.         //static block #4
  94.         System.out.println("10. Initializing: static block #4 (Child)");
  95.         sleep();
  96.     }
  97.  
  98.     {
  99.         //instance initialize block #3
  100.         System.out.println("17. Initializing: instance block #3 (Child)");
  101.         sleep();
  102.     }
  103.  
  104.     private final int instanceLength = initLength();
  105.  
  106.     {
  107.         //instance initialize block #4
  108.         System.out.println("19. Initializing: instance block #4 (Child)");
  109.         sleep();
  110.     }
  111.  
  112.     public Child() {
  113.         System.out.println("20. Executing body of Child constructor (Child)");
  114.         sleep();
  115.     }
  116.  
  117.     // порядок методов не имеет значения, они отработают тогда и только тогда
  118.     // когда будут вызваны явно в коде
  119.  
  120.     private String initKey() {
  121.         System.out.println("16. Initializing: instance variable - instanceKey (Child)");
  122.         sleep();
  123.         return "Magic Key";
  124.     }
  125.  
  126.     private int initLength() {
  127.         System.out.println("18. Initializing: instance variable - instanceLength (Child)");
  128.         sleep();
  129.         return 20;
  130.     }
  131.  
  132.     private static String initAddress() {
  133.         System.out.println("7. Initializing: static - String address (Child)");
  134.         sleep();
  135.         return "Angela";
  136.     }
  137.  
  138.     private static int initCount() {
  139.         System.out.println("9. Initializing: static - int count ");
  140.         sleep();
  141.         return 25;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement