Thursday 10 January 2013

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();

2 comments:

  1. 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

    ReplyDelete
  2. 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.

    ReplyDelete