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 'New operator' - 4 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. In first case we are trying to initialize Inner class object using the instance of Outer Class whereas in second case we are trying to initialize the Inner class object directly using the Outer class name. In second case , Inner class is "static inner class" as we cannot access "non static inner class" using Classname alone. In first case, the inner class could be either "static inner class" or "non static inner class". | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  inner classes  inner class   static inner class   new operator | ||||
| ||||
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 | ||||