Technical Interview

Home
Added Recently
Data Structures
Puzzles
Google & Microsoft
C/C++ Questions
Java Interview Questions
Quantitative Problems
Algorithms
Featured Articles
Amazon Interview Question
Compaq Interview Question
Technical Interview
Interview Process
Introduction Questions
Object Oriented
Google Pages
Fundamental Questions
Resume Tips
Links
Contact Us
Submit Question/Answer

Question:

Describe what happens when an object is created in Java








Answer:  Several things happen in a particular order to ensure the object is constructed properly:

  •  Memory is allocated from heap to hold all instance variables and implementation-specific
data of the object and its superclasses.  Implementation-specific data includes pointers to
class and method data.
  • The instance variables of the objects are initialized to their default values.
  • The constructor for the most derived class is invoked.  The first thing a constructor does is call the constructor for its superclasses.  This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
• Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed.  Then the body of the constructor is executed.  Thus, the constructor for the base class completes first and constructor for the most derived class completes last.  The methods of the class and its parent hierarchy are made available.  Lastly,the address of the object is returned.