Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Object creation' - 6 question(s) found - Order By Newest | ||||
Frequently asked in Infosys and HCL Technologies ( Based on 2 feedback ) | ||||
| ||||
Ans. Using new operator - new xyzClass() Using factory methods - xyzFactory.getInstance( ) Using newInstance( ) method - (Class.forName(xyzClass))emp.newInstance( ) By cloning an already available object - (xyzClass)obj1.clone( ) | ||||
Sample Code for object initialization using clone Sample Code for object initialization using getInstance | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   object creation   new operator   class.forname   cloning   ebay Asked in 9 Companies expert | ||||
| ||||
Ans. 1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data. 2. The instance variables of the objects are initialized to their default values. 3. 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. 4. 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. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   memory management   object creation   advanced   ebay | ||||
| ||||
Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   constructor   object creation intermediate   rare | ||||
| ||||
Ans. new operator is used to statically create an instance of object. newInstance() is used to create an object dynamically ( like if the class name needs to be picked from configuration file ). If you know what class needs to be initialized , new is the optimized way of instantiating Class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   object instantiation   object creation   class.forname   newinstance   new operator   difference between   advanced intermediate | ||||
| ||||
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   constructor   object creation   default constructor   getinstance   new operator | ||||
| ||||
Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   constructor   object creation   default constructor   super | ||||
Try 1 Question(s) Test | ||||