JAVA program execution sequence (static code block "non-static block" static method "constructor)

Summary: static code block is always executed first.

          Non-static block of code, like non-static method, with the object concerned. Only non-static block of code before the constructor.

          Parent non-static block, the constructor is finished (corresponding parent class object initialization is completed), it starts executing code blocks and non-static constructors subclasses.

 

================================================================================

The same point: all constructors and before execution in the JVM loads class can be defined in a plurality of classes,

    Usually some static variables in the code block assignment.

Different points: a static block of code before the code block non-static

( Static block of code -> non-static block -> Constructor ).

    Static block of code is executed only when the first class loader is loaded once and then not performed, rather than static code block is executed once every time new . Non-static block may be defined (but little effect) in the conventional method; and not a static block.

 

JVM executes the static loading class code block, if a plurality of static block, the JVM sequentially executes them in the order they appear in the class , each code block is executed only once.

Example 1:

  1. public class PuTong {
  2. public PuTong(){
  3. . System out.print ( "default constructor ->!");
  4. }
  5.  
  6. // non-static block
  7. {
  8. . The System Out.print ( "non-static block ->!");
  9. }
  10.  
  11. // static code block
  12. static{
  13. . The System Out.print ( "static block of code ->!");
  14. }
  15.  
  16. public static void test(){
  17. {
  18. . The System Out.println ( "conventional method of code blocks!");
  19. }
  20. }
  21. }
  22.  
  23. // test class
  24. public class TestClass {
  25.  
  26. /**
  27. * The difference between the two new static and non-static implementation of block
  28. */
  29. public static void main(String[] args) {
  30. PuTong c1 = new PuTong();
  31. c1.test();
  32.  
  33. PuTong c2 = new PuTong();
  34. c2.test();
  35. }
  36. }
  37.  
  38. /*
  39. Run output is:
  40. Static code block! -> non-static block of code! -> default constructor! -> conventional method of code blocks!
  41. Non-static block of code! -> default constructor! -> conventional method of code blocks!
  42. */



There are two main points:

1, the parent class, subclass of non-static initialization block when?

2, which is the parent class method when calling overridden method, in the end executed?

Example 2:

  1. package tags;
  2.  
  3. public class Child extends Father{
  4.  
  5. static {
  6. System. out.println("child-->static");
  7. }
  8.  
  9. private int n = 20;
  10.  
  11. {
  12. System. out.println("Child Non-Static");
  13. n = 30;
  14. }
  15.  
  16. public int x = 200;
  17.  
  18. public Child() {
  19. this("The other constructor");
  20. System. out.println("child constructor body: " + n);
  21. }
  22.  
  23. public Child(String s) {
  24. System. out.println(s);
  25. }
  26.  
  27. public void age() {
  28. System. out.println("age=" + n);
  29. }
  30.  
  31. public void printX() {
  32. System. out.println("x=" + x);
  33. }
  34.  
  35. public static void main(String[] args) {
  36. new Child().printX();
  37. }
  38. }
  39.  
  40. class Father {
  41.  
  42. static {
  43. //System.out.println("n+"+n);
  44. // When n is defined at the following will be prompted Can not reference a field before it is defined,
  45. // must be moved n are as defined above, the output can
  46. System. out.println("super-->static");
  47. }
  48.  
  49. public static int n = 10;
  50. public int x = 100;
  51.  
  52. public Father() {
  53. System. out.println("super's x=" + x);
  54. age();
  55. }
  56.  
  57. {
  58. System. out.println("Father Non-Static");
  59. }
  60.  
  61. public void age(){
  62. System. out.println("nothing");
  63. }
  64. }

 

 result:

 

super-->static

child-->static

Father Non-Static

super's x=100

age=0

Child Non-Static

The other constructor

child constructor body: 30

x=200

 

 

Parent class static block of code -> subclass static code block

-> parent class non-static block -> parent class constructor

-> non-static block subclass -> subclass constructor

 

 

 

java, when using the new operator time to create an instance of a class, and the space assigned starting member variable is initialized to the default value , this does not mean to note variable is initialized to an initial value at a variable definition, but to shaping assigned 0, a null string assigned to this different C ++, (student.name = null, student.age = 0)

 

Then enter the class constructor.

In the constructor function, we must first check whether this or super call , this call is completed calls between the present class constructor itself, super call is called through the parent class. Both can appear only one, and only as an occurrence of the constructor. Jump to realize the program when calling this and super and instead perform this constructor is called or the super constructor.

In this and finished super, variable initialization program executed instead work undertaken in the class definition.

This is finished, the constructor is executed in the rest of the code.

 

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/11433566.html