FirstThe following sections are excerpted from thinking in Java
It is necessary to summarize the object creation process here. Consider a class named dog:
(1) An object of type dogFirst timeStatic Method/static field of the Dog class during creationFirst timeDuring access, the Java interpreter must find dog. Class (search in the preset class path ).
(2) After finding dog. Class (it creates a class object, which will be learned later), all its static initialization modules will run. Therefore, static initialization only occurs once-when the class object is loaded for the first time.
(3) when creating a new dog (), the building process of the dog object first allocates enough storage space for a dog object in the memory heap.
(4) the bucket is cleared to zero, and all basic types in dog are set to their default values (0 is used for numbers, and the equivalent settings of Boolean and char ).
(5) All initialization tasks that occur during field definition are executed (important ).
(6) execute the builder. This may actually require a considerable number of operations, especially when inheritance is involved.
Of course, there is also a static initialization block: static {here is the assignment action}
Like other static initialization, this code is only executed once-when an object of that class is generated for the first time, or when accessing a static member of that class for the first time (even if the class object is never generated)
SecondDescription: events that occur when the subclass constructor is called.
Let's look at the first example below:
// Print the result boardgame constructor, no argument
// Chess Constructor
Class boardgame
{
Boardgame ()
{
System. Out. println ("boardgame constructor, no argument ");
}
Boardgame (int I)
{
System. Out. println ("boardgame constructor, int argument ");
}
}
Public class constructini extends boardgame
{
Constructini ()
{
System. Out. println ("Chess constructor ");
}
Public static void main (string [] ARGs)
{
Constructini x = new constructini ();
}
}
If you changeConstructlni constructor implementation:
Constructini ()
{
Super (11 );
System. Out. println ("Chess constructor ");
}
The output is: boardgame constructor, int argument
Chess Constructor
Individual test results:
Super (...) is called implicitly if you do not call it ()
That is, constructors without Parameters
Finally, let's look at an example:
Public class constructini
{
Public static final constructini instance = new constructini ();
Private Final int beltsize;
Private Static final int current_year = calendar. getinstance (). Get (calendar. year );
Private constructini ()
{
Beltsize = current_year-1930;
}
Public int beltsize ()
{
Return beltsize;
}
Public static void main (string [] ARGs)
{
System. Out. println ("Elvis wears a size" + instance. beltsize () + "belt .");
}
}
Note: When the instance is being used, current_year has not had time for the instance, so current_year is still 0, so beltsize =-1930
The current_year instance is 2007 (2007 when I wrote this blog), but it is too late. The beltsize cannot be updated and will not be automatically updated.
Running result:
Elvis wears a size-1930 belt.