Thursday, July 2, 2015

Technical questions and Answers

Tags: #Java, #JavaInterview, #InterviewQuestions

  • How to create an Object in Java without using new() function ?
         
          There are different ways in Java to allow instantiate an Object, few of them are listed below

  1.  newInstance() method of Class                                                                                                                                                                                                                                                                    Class class = Class.forName(MyClass);                                                                                MyClass myObject = (Object) class.newInstance();                                                                             
  2.  clone() method of Object class                                                                                                                                                                                                                                                                   MyClass newObject  = myObject.clone();                                                                                         
  3.  Deserialization of any Object                                                                                                                                                                                                                                                                       ObjectInputStream ois = new ObjectInputStream(System.in);                                 MyClass newObject = ois.readObject();                                                                                                            .