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


8 comments :

  1. give me an explanation

    ReplyDelete
  2. And this.getClass().getClassLoader().loadClass(“com.amar.myobject”).newInstance(); as well.

    ReplyDelete
  3. here another blog is also good explaination ,Please go through this blog http://adnjavainterview.blogspot.in/2014/06/how-many-ways-to-create-object-in-java.html

    ReplyDelete
  4. Here one more blog is there please visit it for better explanation. http://javabeginnerstutorial.com/category/core-java-tutorial/page/3/

    ReplyDelete
  5. The blog gave me idea to create objects in java.The methods are explained with clarity which helped me understand the object creation easily
    Java Training in Chennai

    ReplyDelete
  6. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

      Delete
  7. Good and informative post.... it is very useful for me to learn and understand as a initial professional.... keep rocks and updating...

    Java Training in chennai | Java Course in chennai

    ReplyDelete