Member initialization
Java makes every effort to ensure that all variables are properly initialized before they can be used.
For local variables of a method, Java implements this assurance in the form of a compile-time error, such as:
1 void f () 2 {3 int i; There will be an error at compile time, uninitialized 4 }
However, if you are a data member of a class, the situation will be different. Each basic type data member of the class guarantees a default initial value, typically 0, and the Boolean is false, and the reference type is null.
Specify initialization
(1 The Write variable equals how much if it is defined directly in the class (this is not allowed in C + +) such as,
Public class initialv{ int a=1; char c = ' C '; // ... Wait }
Constructor initialization
Can be initialized with a constructor. However, it should be noted that automatic initialization cannot be prevented, and it will occur before the constructor.
Public class counter{ int i; Counter () { i=7; }}
Here, I will first be set to 0, then become 7. For so the base type and object reference, including variables that have been specified as initial values when defined. This is a situation that has been established.
Order of initialization
Within the class, the order in which the variables are defined determines the order in which they are initialized. Even if the variable definitions are scattered between the method definitions, they will still be initialized before any methods (including constructors) are called . The following example
classwindow {window (intMarker) {print ("window (" + marker + ")"); }}classHouse {Window W1=NewWindow (1);//before ConstructorHouse () {//Show that we ' re in the constructor:Print ("House ()"); W3=NewWindow (33);//Reinitialize W3} Window W2=NewWindow (2);//After constructor voidF () {print ("F ()")); } Window W3=NewWindow (3);//At End} Public classorderofinitialization { Public Static voidMain (string[] args) {House H=NewHouse (); H.F (); //shows that construction was done }} /*Output: Window (1) Window (2) window (3) House () window () f ()*///:~
Initialization of static data
A static member of a class belongs to an instance of a class rather than a class. So, no matter how many objects you create, a static member refers to occupying a storage area. The static keyword cannot be applied to local variables, so it can only be used for fields. The initialization mode is the same as non-static.
To see a very long example, can not understand the direct adjustment behind the analysis.
Import Staticnet.mindview.util.print.*;classBowl {Bowl (intmarker) {Print ("Bowl (" + marker + ")"); } voidF1 (intmarker) {Print ("F1 (" + marker + ")"); }}classTable {StaticBowl BOWL1 =NewBowl (1); Table () {print ("Table ()"); BOWL2.F1 (1); } voidF2 (intmarker) {Print ("F2 (" + marker + ")"); } StaticBowl BOWL2 =NewBowl (2);}classCupboard {Bowl bowl3=NewBowl (3); StaticBowl Bowl4 =NewBowl (4); Cupboard () {print ("Cupboard ()"); BOWL4.F1 (2); } voidF3 (intmarker) {Print ("F3 (" + Marker + ")"); } StaticBowl BOWL5 =NewBowl (5);} Public classstaticinitialization { Public Static voidMain (string[] args) {print ("Creating New Cupboard () in main"); Newcupboard (); Print ("Creating New Cupboard () in main"); Newcupboard (); TABLE.F2 (1); CUPBOARD.F3 (1); } StaticTable Table =NewTable (); StaticCupboard cupboard =Newcupboard ();} /*output: Bowl (1) Bowl (2) Table () F1 (1) Bowl (4) Bowl (5) Bowl (3) cupboard () F1 (2) Creating new Cupboard () in Mainbowl (3) Cupboard () F1 (2) Creating new Cupboard () in Mainbowl (3) cupboard () F1 (2) F2 (1) F3 (1)*///:~
Parse this demo: to execute Main () (static method), the Staticinitialization class must be loaded, then its static domain table and cupboard are initialized, which will cause their corresponding class page to be loaded, And since they also contain static bowl objects, bow is then loaded. Dawned
Theory concludes that static initialization occurs only at the necessary time. If you do not create a Table object and do not reference TABLE.B1 or table.b2,name static bowl B1 and B2 will never be created. They are initialized only when the first table object is created (or it is accessed for the first time by static data) . After that, the static object is not initialized again
An explicit static initialization
Java allows multiple static initialization actions to be organized into a special "static phrase" (also known as a "static Block"), as follows.
Public class Spoon { staticint i; Static { =n; // /:~
Although the code above looks like a method, it is actually just a piece of code that follows the static keyword. Like other static initialization actions, this code executes only once: at the same time, when an object of this class is first generated, or when a static data member belonging to that class is first accessed (even if that class has never been generated).
Non-static instance initialization
A non-static variable used to initialize each object. As follows
classMug {Mug (intmarker) {Print ("Mug (" + marker + ")"); } voidFintmarker) {Print ("F (" + marker + ")"); }} Public classMugs {Mug mug1; Mug Mug2; {MUG1=NewMug (1); Mug2=NewMug (2); Print ("Mug1 & mug2 Initialized"); } mugs () {print ("Mugs ()"); } Mugs (inti) {print ("Mugs (int)"); } Public Static voidMain (string[] args) {print ("Inside Main ()"); Newmugs (); Print ("New Mugs () completed"); NewMugs (1); Print ("New Mugs (1) completed"); }} /*output: Inside main () Mug (1) Mug (2) Mug1 & Mug2 initializedmugs () New Mugs () Completedmug (1) Mug (2) MUG1 & MUG2 initializedmugs (int) New Mugs (1) Completed*///:~
This is something that occurs regardless of which explicit constructor is called. From the output, you can see that the instantiation is executed before the constructor.
"Java programming thought" Reading Notes (ii) Chapter fifth (2)