Java programs initialization process explain
Think Core Java Java initialization process in the overall order did not say, only that the structure of the sequence, the author seems to think that the path many listed confusion. I think it is to understand the process better. So now I combine the learning experience to write specific process:
Process is as follows:
1. Kind of statement, to detect static elements (static element, I will call it so), such as:
Static int x = 1,
(
/ / Block
Float sss = 333.3; String str = "hello";
)
For example, or
(Static
/ / (Static block),
Int x = 2;
Double y = 33.3;
)
If there is static element is the implementation of the first sentence, but only pay attention to the implementation of a static element, in the second category of object creation time, it is not static element to the implementation of the sentences.
2. Show for the launch of the operation of such category, where category for the launch of operation, the implementation of main () method, the corresponding statement Statements
3. Category unless it is up and running, according to the layout of the code sequence of non-static element to the implementation of the code block and assign values to variables.
4. Final performance constructor, if the constructor is invoked inside this keyword (Note that if you call to consider other construction method, it should be in the forefront of this writing, or cause an error), then the corresponding first call the main construction method, called End after the implementation of their remaining sentence.
/ ** *//**
*
* @ Author livahu
* Created on September 6, 2006, 17:00 pm
* /
… (Class FirstClass
FirstClass (int i) (…
System.out.println ( "FirstClass (" + i + ")");
)
Void useMethod (int k) (…
System.out.println ( "useMethod (" + k + ")");
)
)
… (Class SecondClass
Static FirstClass fc1 = new FirstClass (1);
FirstClass fc3 = new FirstClass (3);
(Static …
FirstClass fc2 = new FirstClass (2);
)
(…
System.out.println ( "SecondClass's block, this block is not static block.");
)
SecondClass () (…
System.out.println ( "SecondClass ()");
)
FirstClass fc4 = new FirstClass (4);
)
… (Public class InitiationDemo
SecondClass sc1 = new SecondClass ();
(…
System.out.println ( "Hello Java World!");
)
Public static void main (String [] args) (…
System.out.println ( "Inside main ()");
SecondClass.fc1.useMethod (100);
InitiationDemo idObj = new InitiationDemo ();
)
Static SecondClass sc2 = new SecondClass ();
)
The result:
FirstClass (1)
FirstClass (2)
FirstClass (3)
SecondClass's block, this block is not static block.
FirstClass (4)
SecondClass ()
Inside main ()
UseMethod (100)
FirstClass (3)
SecondClass's block, this block is not static block.
FirstClass (4)
SecondClass ()
Hello Java World!
Tags: Initialization, Process






