Results 1 to 4 of 4

Thread: How many ways to create an object for class

  1. #1
    Join Date
    Apr 2009
    Posts
    107

    How many ways to create an object for class

    Hello Friends,

    I am working with JAVA, and i used to create an Object for a class in a traditional way, that just simply, whatever class I create, i create the object in the Void main and through which i access the class.This is the simple the way i create the object but can anyone suggest is there any other way by which i create the class and access them.

    Thanks for your suggestions.

  2. #2
    Join Date
    Jan 2009
    Posts
    140

    Re: How many ways to create an object for class

    Hey as per my knowledge
    1. using new keyword
    2. class.forName() : provided it has default public constructor called up
    3. even deserialization creates an object from its serialized form

  3. #3
    Join Date
    Dec 2008
    Posts
    202

    Re: How many ways to create an object for class

    As my concern we can create an object in java by 4 ways.

    1.By using new operator

    Integer i=new Integer();


    2.Using Class

    Class myclass=Class.forName(MyClassName);
    myclass.newInstance();


    3.Using shallow clonig

    4.Using DeepCloning i.e Serialization

  4. #4
    Join Date
    Mar 2008
    Posts
    232

    Re: How many ways to create an object for class

    There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

    1. Using new keyword
    This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

    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("subin.rnd.MyObject").newInstance();

    3. Using clone()
    The clone() can be used to create a copy of an existing 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.

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

    Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

Similar Threads

  1. Ways to disable object snap in Word 2010
    By M@ndy in forum Windows Software
    Replies: 2
    Last Post: 04-12-2011, 08:03 PM
  2. Replies: 6
    Last Post: 06-06-2011, 01:34 AM
  3. Different ways to initiate an object
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 11-03-2010, 12:46 PM
  4. What is the Difference between Class and Object?
    By RupaliP in forum Software Development
    Replies: 5
    Last Post: 28-02-2009, 07:03 PM
  5. Class Object in C++
    By paintbox in forum Software Development
    Replies: 0
    Last Post: 25-10-2008, 01:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,803,811.44830 seconds with 16 queries