Different ways of creating object in Java

By Vinay | January 14, 2010 | 3,713 views
Category Java

  Share


About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Oracle ADF,Spring,hibernate,J2ee,PL/sql,Apps for 2+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion. http://www.linkedin.com/in/vinaykumar2 Read more from this author


1. Using new keyword
This is the most common way to create an object in java.In This we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.

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( vinay.abc.MyObject ).newInstance();

vinay.abc.MyObject – is the class name

3. Using clone()

The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone 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. It will uses the ‘new’ operator internally and always calls the default constructor.

 ObjectInputStream inStream new ObjectInputStream(anInputStream );
MyObject object (MyObject) inStream.readObject();

5. Using class loader

one more is through creation of object using classloader
like

this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance();

pimp it

  • Delicious
  • Yahoo Buzz
  • Digg
  • DZone
  • Facebook
  • LinkedIn
  • Twitter
  • Share/Bookmark
Read more post on

  Share

9 comments | Add One

Comments

  1. nixNo Gravatar - 01/15/2010 at 3:08 pm

    I think java object it’s very interested for newbe. thanks



  2. K BooNo Gravatar - 01/15/2010 at 4:05 pm

    Not one of those examples will compile. Interesting and informative topic, but syntax is important!



  3. ArturNo Gravatar - 01/16/2010 at 4:01 am

    What about using Reflection API ? It is still common approach



  4. PravinNo Gravatar - 01/17/2010 at 10:53 pm

    2 and 5 above are same.
    this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance();
    is same as
    Class.forName(com.amar.myobject)

    And important point about clone() method is that, it would only work for those classes which implement the Cloneable interface.



  5. DerrickNo Gravatar - 01/18/2010 at 5:48 am

    Both Class.forName() and getClassLoader().loadClass() take strings as parameters.



  6. vinayNo Gravatar - 01/27/2010 at 4:21 am

    Adding new way of creating through factories.
    Full details in -http://www.javaworld.com/javaworld/javaqa/2001-05/02-qa-0511-factory.html

    Other uses you may think of is with DocumentBuilderFactory when working with XML in Java. A factory helps to hide implementation details from public interfaces and support polymorphism as you only need to know the kinds of behaviours that the object created by the factory adheres to and not how the factory creates it.



Trackbacks

  1. PimpThisBlog.com
  2. Tweets that mention Different ways of creating object in Java | TechArtifact -- Topsy.com
  3. Java bookmark links, Jan 2010 – Week 3 | Bookmark-link

Leave a Comment

Name:

E-Mail :

Website :

Comments :