We use the following class to illustrate the order in which a basic Java class is run:
- Public class Demo{
- Private String name;
- Private int age;
- Public Demo(){
- Name = "Micro School";
- Age = 3;
- }
- Public static void main(String[] args){
- Demo obj = New demo();
- System. Out. Theage of println (obj. Name + "is" + obj.);
- }
- }
The basic run order is:
- First, run to line 9th, which is the entrance to the program.
- Then run to line 10th, where a new demo is called to call the demo's construction method.
- Run To line 5th, note: Many people may think that the next line should be run 6th, wrong! To initialize a class, you must first initialize its properties.
- So run to line 2nd, then line 3rd.
- After the initialization of the property, the constructor is returned to the code, which is the 6th line and the 7th line.
- Then the 8th line indicates that the new demo instance is complete.
- Then go back to the main method and execute line 11th.
- Then the 12th line, and the main method executes.
As a programmer, it should be clear that the basic process of operation, otherwise confused, not conducive to writing code, but also not conducive to technical development.
Basic run order of the 5.Java class