Mastiff class
/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicMastiff () {System.out.println ("Mastiff"); } {System.out.println ("Block"); } Static{System.out.println ("Static Block"); } Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); }}
Dog class
/*** *publicclass dog {public Dog () { System.out.println ("Dog");} } </span>
The result of the operation is:
Static block Dog Block Mastiff |
In other words, in our program, when instantiating a class object, the order of operation is:
- Static block
- Parent class constructor
- Blocks in this class
- Constructors for this class
Can we go further, if there are blocks and static blocks in the parent class?
Dog class improved after source code
/*** *publicclass dog {public Dog () { System.out.println ("Dog"); } Static { System.out.println ("Super static Block"); } { System.out.println ("Super Block");} }
Mastiff improved source code
/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicMastiff () {System.out.println ("Mastiff"); } {System.out.println ("Block"); } Static{System.out.println ("Static Block"); } Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); }}
The result of the operation is:
Super static block Static block Super Block Dog Block Mastiff |
In other words, the order of operation at this time is:
- Parent class static Block
- Self-static block
- Parent Class Block
- Parent class constructor
- Self block
- Self-constructor
Well, knowing the order of the run, then what is this for? This is going to start with the loading mechanism and instantiation mechanism of the class in the JVM, here because of the subject matter, not discussed first, interested students can check their own information. |
Let's discuss the second question, the value of a variable, where it might be determined??
- Inherit the value from the parent class (including: 1. A member variable that is a parent class has been assigned a value of 2. Assign a value in the parent class's block 3. Assigning values in the constructor of the parent class)
- Assigning a value to a constructor
- Assigning values in a block
- assigning values in a method call
Now suppose in our just example that there is a variable type that represents the breed of dog
/*** *publicclass Dog {public String type= " The value assigned to the parent class member variable "; Public Dog () { System.out.println ("parent class constructor--type-->" +type); Type= "The value assigned by the parent class constructor"; System.out.println ("parent class constructor----Type--->" +type); } { System.out.println ("Block---type--->" +type); Type= "The value assigned by the parent class block";} }
/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicString type= "value assigned to member variable"; PublicMastiff () {System.out.println ("Constructor---Type--->" +type); Type= "constructor-assigned value"; } Public voidsay () {System.out.println ("Say---type---->" +type); } {System.out.println ("Block---type--->" +type); Type= "Value of block assignment"; } Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); Mastiff.say ()</span><span style= "Font-size:medium;" >;</span><span style= "Font-size:medium;" > }}
The results of the implementation are as follows:
Block---The value assigned to type---> Parent class member variable Parent class constructor--type--> the value assigned to the parent class block Parent class constructor----value assigned to type---> Parent class constructor Block---value assigned to type---> member variable Constructor---The value assigned to type---> block Say---type----> constructor-assigned value |
The answer is clearly that the order of assignment is:
- Parent class Member variable assignment
- Parent Class block Assignment
- Parent class constructor Assignment value
- Assigning values to self-member variables
- Self-block assignment
- Self constructor Assignment
In combination with the order of execution in the program we said earlier, this is clearly well understood:
1. Member variable assignment >>> block Assignment >>> constructor assignment
2. Parent class block >> parent class constructor >> self block >> self constructor
Also because a member variable is not possible to assign a value in a static variable, and the previous program execution sequence is known
Static blocks >> Blocks
Therefore, the program's assignment step is
- Static variable assignment for parent class
- Static variable assignment of its own value
- Parent class Member variable assignment
- Parent Class block Assignment
- Parent class constructor Assignment value
- Assigning values to self-member variables
- Self-block assignment
- Self constructor Assignment
Java Program Execution Order