Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-04-2009
Member
 
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.
Reply With Quote
  #2  
Old 25-04-2009
Member
 
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
Reply With Quote
  #3  
Old 25-04-2009
Member
 
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
Reply With Quote
  #4  
Old 25-04-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How many ways to create an object for class"
Thread Thread Starter Forum Replies Last Post
Ways to disable object snap in Word 2010 M@ndy Windows Software 2 04-12-2011 08:03 PM
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 01:34 AM
Different ways to initiate an object Angelica Maria Software Development 5 11-03-2010 12:46 PM
What is the Difference between Class and Object? RupaliP Software Development 5 28-02-2009 07:03 PM
Class Object in C++ paintbox Software Development 0 25-10-2008 01:13 PM


All times are GMT +5.5. The time now is 02:29 AM.