How many ways to create an object in java??? plz give an example.

Questions by amitbca32000

Showing Answers 1 - 23 of 23 Answers

jade

  • Feb 28th, 2006
 

hello

        there r 4 ways to creat e objects.

           1.using new operator

                     classname ref=new classconstructor()

           2.using class.forName("classname").newInstance()

           3.using clone

           4.using serializable

  Was this answer useful?  Yes

Dick Winters

  • Mar 12th, 2006
 

hi,

clould you tell more about the second to fourth way?

thanks

  Was this answer useful?  Yes

anjan

  • Nov 27th, 2006
 

Hi my name is anjan,

i need clear discription about creation of objects

using no of ways, with example....

  Was this answer useful?  Yes

kuppula.kiran

  • Jun 21st, 2007
 

Serialization?
You can't create an object that way. You can use deserialization to create an object from a serialized form

  Was this answer useful?  Yes

jkathiravan

  • Sep 10th, 2009
 

There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

MyObject object = new MyObject();

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.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

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

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.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();

Thanks and Regards,

Kathiravan Jayachandran

Mumbai

  Was this answer useful?  Yes

Djava

  • Sep 12th, 2009
 

package objectCreation;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestObjectCreation implements Cloneable,Serializable{

    /**
     * @param args
     */
    
    public void method1(){
        System.out.println("inside method1");
    }
    
    
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, CloneNotSupportedException, IOException {

        //way no 1
        Class classObj  = Class.forName("objectCreation.TestObjectCreation");
        TestObjectCreation obj1 = (TestObjectCreation)classObj.newInstance();
        obj1.method1();
        
        //way no 2
        TestObjectCreation obj2 = new TestObjectCreation();
        obj2.method1();
        
        //using clone
        TestObjectCreation obj3 = (TestObjectCreation)obj1.clone();
        obj3.method1();
        
        //from deserialization
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj1);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        TestObjectCreation obj4 = (TestObjectCreation)ois.readObject();
        obj4.method1();


    }

}

OUTPUT:
inside method1
inside method1
inside method1
inside method1

  Was this answer useful?  Yes

praveen

  • Aug 8th, 2012
 

1.newInstance()
2.static factory()
3.InstanceFactory()
4.factoryPattern()
5.deserialization
6clonning
7.new keyword.

  Was this answer useful?  Yes

Hari

  • Nov 2nd, 2012
 

Only 4 ways.
1) Class c=new Class(); -->Using new operator
2) Class.forName("Class"); -->Using class.forName
3) NumberFormat object=NumberFormat.getNumberInstance(); -->Using Factory methods
4) Class c=new Class();
Class c2=(Class)c.clone(); -->using Cloning

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions