Ads Sponsored By Google :)

Monday 22 July 2013

Different Ways of Creating Object in java with Example


Different ways of creating object in java, ways of creating object in java, methods of creating object in java, example of creating object in java, types of creating object in java, how many types of creating object in java, how many ways to create object in java with examples, ways of creating objects in java with example


Different Ways of Creating An Object in Java 

  This Article about How to Create a object in java ? In most of the Interviews we will get number of ways of creating Objects in java. Most of them don’t know how many ways are for creation of an Obect  In Java. There are Four ways of creating object in java.

1) By Using new keyword
2) By Using Class.forName()
3) By Using clone()
4) By Using object deserialization

Detailed explanation on different ways to create a object in java:

Consider a class as shown below:
package com.objectcreation;
public class CreationOfObjects implements Cloneable {
    public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException, CloneNotSupportedException {
        // 1) Object created using the new keyword
        CreationOfObjects objectNew = new CreationOfObjects();
        // 2) Object created using Class.forName
        CreationOfObjects objectClassForName = (WaysToCreateObject) Class.forName(
                "com.objectcreation.WaysToCreateObject").newInstance();

        // 3)Object created using clone
        CreationOfObjects objectClone = (WaysToCreateObject) objectNew.clone();
    }
}

No comments:

Post a Comment

LinkWithin