Initialization and cleanup:
Creating objects must not only assign memory but also call constructor. The parameters of constructor are a way to provide parameters that enable your object's initialization.
THIS
Normally, "current object" or "this object" can only be used inside, responsible for returning references to the object to call this method.
Public class flower {INT PETALCOUNT = 0; string s = new string ("null"); Flower (int point) {petalcount = PETALS; System.out.Println ("constructor w / int arg only, petalcount =" Petalcount) ;} Flower (STRING SS) {System.Ost.Println ("Constructor W / String Arg Only, S =" SS); S = SS;} Flower (String S, INT PETALS) {this (Petals); S = S; System.out.Println ("String & Int Args");} Flower () {THIS ("Hi", 47); System.out.Println ("Default Constructor (no args)";} void Print () {system.out.println ("Petalcount =" Petalcount "S =" S);} public static void main (string [] args) {Flower x = new flower (); x.print () }}
The order in which the initialization is determined by each variable in the order of the definition of the class. The definition of variables may be dispersed in all parts of the class definition, and the definition of the method is interlaced with each other, but the initialization of the variables will first prior, even It is a constructor.
Initialization of static data:
Class bowl {bowl (int marker) {system.out.println ("Bowl (" marker ")");} void f (int market) {system.out.println ("f (" marker ") ");}} Class table {static bowl b1 = new bowl (1); table () {system.out.println (" Table () "); b2.f (1);} Void F2 (int marker) { System.out.println ("F2 (" Marker ")") ")") ");} Static Bowl B2 = New Bowl (2);} Class Cupboard {BOWL B3 = New Bowl (3); static bowl bowl (4); 4 ); Cupboard () {system.out.println ("CupBoard ()"); b4.f (2);} void f3 (int marker) {system.out.println ("F3 (" marker ")" } Static bowl b5 = new bowl (5);} public class staticinitialization {static test monitor = new test (); static table t2 = new table (); static cupboard t3 = new cupboard (); public static void main String [] args) {system.out.println ("CREANG New Cupboard () in main"); new cupboard (); system.out.println ("CREANG New Cupboard () in main"); new cupboard (); T2.f2 (1); T3.f3 (1); Monitor.expect (new string "{ "Bowl (1)", "Bowl (2)", "Table ()", "F (1)", "Bowl (4)", "Bowl (5)", "Bowl (3)", "Cupboard () "," F (2) "," Creating New Cupboard () In Main "," Bowl (3) "," Cupboard () "," F (2) "," CREATING New Cupboard () in main " "Bowl (3)", "Cupboard ()", "F (2)", "F2 (1)", "F3 (1)"});}} The Static member will only initialize when needed. If the Static member has not been initialized, initialization will process its Static member to process non-Static objects.
Clean up: Finalization and garbage collection Java garbage collection only knows the release of New from New.
Java: 1. The object will not be recycled by the garbage collector 2. Garbage recycling is not destructure 3. garbage collection is only related to memory.
As long as the JVM has not yet lived through the bottom of memory, it will not waste time to recycle garbage to resume memory.