Java object initialization

Source: Internet
Author: User

Java programming ideology (version 4th) note

Frog. java
// Cleanup and inheritance
Class Characteristic {
Private String s;
Public static String static_Chara;
Static {
Static_Chara = "***** Characteristic static Init! ";
System. out. println (static_Chara );
}
Characteristic (String s ){
This. s = s;
System. out. println ("Creating Characteristic" + s );
}
Protected void dispose (){
System. out. println ("disposing Characteristic" + s );
}
}
Class Description {
Private String s;
Description (String s ){
This. s = s;
System. out. println ("Creating Description" + s );
}
Protected void dispose (){
System. out. println ("disposing Description" + s );
}
}
Class LivingCreature {
Public static String static_LC;
Static {
Static_LC = "***** LivingCreature static Init! ";
System. out. println (static_LC );
}
Private Characteristic p = new Characteristic ("is alive ");
Private Description t = new Description ("Basic Living Creature ");
LivingCreature (){
System. out. println ("LivingCreature ()");
}
Protected void dispose (){
System. out. println ("LivingCreature dispose ");
T. dispose ();
P. dispose ();
}
}
Class Animal extends LivingCreature {
Public static String static_Animail;
Static {
Static_Animail = "***** Animal static Init! ";
System. out. println (static_Animail );
}
Private Characteristic p = new Characteristic ("has heart ");
Private Description t = new Description ("Animal not vegetable ");
Animal (){
System. out. println ("Animal ()");
}
Protected void dispose (){
System. out. println ("Animal dispose ");
T. dispose ();
P. dispose ();
Super. dispose ();
}
}
Class Amphibian extends Animal {
Private Characteristic p = new Characteristic ("can live in water ");
Private Description t = new Description ("Both water and land ");
Amphibian (){
System. out. println ("Amphibian ()");
}
Protected void dispose (){
System. out. println ("Amphibian dispose ");
T. dispose ();
P. dispose ();
Super. dispose ();
}
}
Public class Frog extends Amphibian {
Private Characteristic p = new Characteristic ("Croaks ");
Private Description t = new Description ("Eats Bugs ");
Public Frog (){
System. out. println ("Frog ()");
}
Protected void dispose (){
System. out. println ("Frog dispose ");
T. dispose ();
P. dispose ();
Super. dispose ();
}
Public static void main (String [] args ){
System. out. println ("main ()");
Frog frog = new Frog ();
System. out. println ("Bye! ");
Frog. dispose ();
}
}
Output:
D: \ Javawork \ thinkinjava \ ch5> java Frog
* *** LivingCreature static Init!
* *** Animal static Init!
Main ()
* *** Characteristic static Init!
Creating Characteristic is alive
Creating Description Basic Living Creature
LivingCreature ()
Creating Characteristic has heart
Creating Description Animal not vegetable
Animal ()
Creating Characteristic can live in water
Creating Description Both water and land
Amphibian ()
Creating Characteristic Croaks
Creating Description Eats Bugs
Frog ()
Bye!
Frog dispose
Disposing Description Eats Bugs
Disposing Characteristic Croaks
Amphibian dispose
Disposing Description Both water and land
Disposing Characteristic can live in water
Animal dispose
Disposing Description Animal not vegetable
Disposing Characteristic has heart
LivingCreature dispose
Disposing Description Basic Living Creature
Disposing Characteristic is alive

Object creation process (no inheritance): Assuming the class name is Obj
When Obj. class is required
1. Load class files
2. initialize static
New Obj ()
3. Allocate space and clear
4. initialize the field Definition
5. Obj (), constructor.

When there is inheritance. As shown in the preceding example.
The inheritance relationship is Frog → Amphibian → Animal → LivingCreature.
1. The main method is in Frog. Therefore, first load Frog. class, find the inheritance relationship (extends), and continue to load its base class.
2. After loading, first perform the static initialization action of the base class, and then perform static initialization layer by layer.
3. Execute the main function.
4. new Frog ()
5. Perform initialization of the underlying class layer by layer and call its constructor. Then execute the underlying base class initialization and constructor.

New Characteristic () is encountered during initialization. Load Characteristic. class, static initialization, (initialized at the field definition), constructor.

So output:

* *** LivingCreature static Init!
* *** Animal static Init!
Main ()
* *** Characteristic static Init!
Creating Characteristic is alive
Creating Description Basic Living Creature

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.