How many ways to create object in java?

There are different ways to create object in java as follows:–

1. Using new keyword
we can create the instance normally using new operator which is called as static instance creation.
Hello hello =  new Hello();

2.using Class.forName()
we can create the instance dynamically without using new operator as follow
Hello hello=(Hello)Class.forName(“com.bikash.Hello”).newInstance();
or
Class cls = Class.forName(“com.bikash.Hello”);
Hello hello = (Hello)cls.newInstance();

3.using clone().
clone() method can be used to copy of the existing object.
Hello hello=new Hello();
Hello hello1=(Hello)hello.clone();

4.using object deserialization.
deserializion is the process of creating the new object on the remote mechine from its serialize form.
ObjectInputStream ois =new ObjectInputStream();
Hello hello = (Hello)ois.readObject();

Advertisements

2 thoughts on “How many ways to create object in java?

  1. matasari January 18, 2013 at 8:28 pm Reply

    That is really attention-grabbing, You’re a very professional blogger. I’ve joined your rss feed and stay up for in search of more of your magnificent post. Additionally, I have shared your web site in my social networks

  2. Cheap Neopoints January 20, 2013 at 12:36 am Reply

    Thank you, I’ve just been looking for information about this subject for a while and yours is the best I have found out till now. But, what in regards to the conclusion? Are you positive about the supply?|What i don’t realize is if truth be told how you’re not really a lot more smartly-liked than you might be now. You are very intelligent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: