Monday, July 23, 2012

How object in Java is initialized

1. A class is loaded when it is used for the first time, it is loaded from the class file (Ex. Student.class). If this class has a base class, the base class will be loaded before this class. 

2. Next, the static initialization in the base class is performed which initialize the static fields of the base class, and then the static initialization of this (derived) class kicks in, which makes sure that the base class static members have been initialized properly in case that derived class static initialization depend on them. Static initialization takes place only once.

3. Storage is allocated for this new object on the heap.

4. Storage is wiped to zero, all primitive fields of the base class and the derived class are set to their default values, which is zero or false, all reference fields are set to  null.

5. If there are any initialization at the definition of fields of the base class, it should take place at this point.

6. Execute the constructor of the base class.

7. The same steps 5 - 6 occur for the derived class.

0 comments:

Post a Comment