Compare hotel prices and find the best deal - HotelsCombined.com

Tuesday, January 6, 2015

How many ways to create an object in java?

 There are 5 ways to create an object in java(i know).

1) Using new keyword
            This is the most common way to create an object in java.  Almost 99% of 
   objects are created in this way. 
    e.g     Employee emp=new  Employee();

2) Using Class.forName()
          If we know the name of the class & if it has a public default constructor we 
 can create an object in this way.
         Employee ob = (Employee)  
                                        Class..forName("com.adn.Employee").newInstance();

3) Using clone()
        
The clone() can be used to create a copy of an existing object.  

                 Employee emp=new Employee();
                 Employee emp1=emp.clone();

4) Using object deserialization
         Object deserialization is nothing but creating an object from its serialized form.
                ObjectInputStream  inStream = new   
                                                                   ObjectInputStream(anInputStream);
                                     MyObject object = (MyObject) inStream.readObject();
5) Using xml transformation