GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 495 of 502    Print  
Object Creation
In how many ways we can create an object? Explain with example.


  
Total Answers and Comments: 6 Last Update: June 24, 2009     Asked by: justjiten 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sreedhar dantam
 
In Java objects can be created in 4 ways.

1. using new operator
2. First get class reference either using Class.forname or class loader. On this class, create object using 'newInstance' method.
3. Clone
4. Deserialization

Above answer was rated as good by the following members:
vasu123, akhilmisra, uday bhasker reddy, justjiten, agarwal_nishtha08, sharathe, Auxilius, ybrw13a, vaseem35, salil_sp, sushilkotnala, ahamza59, shirishparmar, zjlovezj, aishwarya.srivastava, hemant17, sonalgauravmahajan, piyushbeli, sandip.samanta, bino75, evelinp, sandeep549
December 19, 2008 14:33:19   #1  
sreedhar dantam Member Since: December 2008   Contribution: 2    

RE: Object Creation
In Java objects can be created in 4 ways.

1. using new operator
2. First get class reference either using Class.forname or class loader. On this class create object using 'newInstance' method.
3. Clone
4. Deserialization

 
Is this answer useful? Yes | NoAnswer is useful 10   Answer is not useful 1Overall Rating: +9    
February 12, 2009 00:05:17   #2  
trivenireddy Member Since: February 2009   Contribution: 2    

RE: Object Creation
We can create an object in 5 ways
1)new operator
2)clone
3)de-serialization
4)Instance of
5)Class.forName

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
February 13, 2009 01:34:59   #3  
vishrutha Member Since: February 2009   Contribution: 1    

RE: Object Creation

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

5. Using class loader
one more is through creation of object using classloader
like


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

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


 
Is this answer useful? Yes | NoAnswer is useful 12   Answer is not useful 1Overall Rating: +11    
March 22, 2009 15:22:50   #4  
Auxilius Member Since: March 2009   Contribution: 1    

RE: Object Creation
I agree with the answer given by vishrutha with few modifications.

  • Part 2 and the last is very similar to each other.
  • Deserialization can not be called as way of object creation.
  • Clone is useful for normal objects but not for composite objects.
From the perspective of design patterns we have six ways of object creation by GoF. But that is all about design patterns. If you ever heard about singleton objects (one of the six) you should read GoF creational patterns.

 
Is this answer useful? Yes | NoAnswer is useful 4   Answer is not useful 0Overall Rating: +4    
April 20, 2009 10:16:59   #5  
vaseem35 Member Since: April 2009   Contribution: 1    

RE: Object Creation

In only one way. That is using 'new' keyword. Without using 'new' we cannot
create object.


Example:

classname cn new classname();


here classname is name of the your class.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
June 24, 2009 08:33:17   #6  
ashish_dhole30 Member Since: January 2009   Contribution: 1    

RE: Object Creation

1.Using New KeyWord
2.Using ClassForName
3.Clone


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape