Summary: static code blocks are always executed first. A non-static block of code, like a non-static method, is related to an object. Only non-static blocks of code are executed before the constructor. A non-static block of code and a constructor for a subclass are only started when the parent class is non-static and the constructor finishes executing (equivalent to the initialization of the parent class object).
The same point: when the JVM loads the class and executes before the construction method executes, you can define more than one in the class, typically assigning some static variables to the code block.
Different points: Static code blocks are executed before non-static blocks of code (static code blocks, non-static code blocks, and construction methods).
A static code block executes only once for the first time, and then no longer executes, rather than a static block of code that executes once per new. A non-static block of code can be defined in a common method (though not very useful), while a static block of code does not. The JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, the JVM executes them sequentially in the order in which they appear in the class, and each block is executed only once.
Example1:
- Public class Putong {
- Public Putong () {
- System.out.print ("default constructor Method! --");
- }
- //non-static code block
- {
- System.out.print ("Non-static code block! --");
- }
- //Static code block
- static{
- System.out.print ("Static code block! --");
- }
- public static void Test () {
- {
- System.out.println ("code blocks in the normal Method!") ");
- }
- }
- }
- Test class
- Public class TestClass {
- /**
- * Distinguish between two times new static and non-static blocks of code execution
- */
- public static void Main (string[] args) {
- Putong C1 = new Putong ();
- C1.test ();
- Putong C2 = new Putong ();
- C2.test ();
- }
- }
- /*
- The results of the run output are:
- Static code block! --Non-static code block! --default constructor Method! --code blocks in the normal method!
- Non-static code block! --default constructor Method! --code blocks in the normal method!
- */
Example2:
- Package tags;
- Public class child extends father{
- Static {
- System.out.println ("child-->static");
- }
- private int n = 20;
- {
- System.out.println ("Child non-static");
- n = 30;
- }
- public int x = 200;
- Public child () {
- This ("The other constructor");
- System.out.println ("Child constructor Body:" + N);
- }
- Public child (String s) {
- System.out.println (s);
- }
- public void Age () {
- System.out.println ("age=" + N);
- }
- public void Printx () {
- System.out.println ("x=" + x);
- }
- public static void Main (string[] args) {
- new Child (). Printx ();
- }
- }
- Class Father {
- Static {
- //system.out.println ("n+" +n);
- //When n is defined below, you will be prompted cannot reference a field before it is defined,
- //So the n definition must be moved above to output
- System.out.println ("super-->static");
- }
- public static int n = 10;
- public int x = 100;
- Public Father () {
- System.out.println ("Super ' s x=" + x);
- Age ();
- }
- {
- System.out.println ("Father non-static");
- }
- public void Age () {
- System.out.println ("nothing");
- }
- }
Results:
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 code block, subclass static code block, parent class non-static code block-parent class constructor-subclass non-static code block-subclass constructor
In Java, when you create an instance object of a class using the new operator, you begin to allocate space and initialize the member variable to the default value, note that this does not mean that the variable is initialized to the initial values at the variable definition, but instead assigns a value of 0 to the shape and assigns null to the string, which is different from C + +. (Student.name = null, Student.age = 0) and then enters the constructor of the class. Inside the constructor, the first thing to check for this or super call is to complete the call between the constructors of the class itself, and the super call is to complete the call to the parent class. The two can only appear in one, and can only appear as the first sentence of the constructor. Implement the program's jump when calling this and super, instead of executing the called this constructor or super constructor. At the end of this and super, the program performs the initialization of the variables at the time of the class definition. This completes the execution of the remaining code in the constructor.
Static code blocks & non-static code blocks & constructors