aspose file tools*
The moose likes Beginning Java and the fly likes ways of instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ways of instantiation" Watch "ways of instantiation" New topic
Author

ways of instantiation

Jeevan Reddy
Ranch Hand

Joined: Nov 10, 2009
Posts: 142
Hi,

I was asked this question which I could not answer.
What are the different ways of instantiation in Java?

One of the ways is using "new" and I stopped at that saying I was not sure.

If anyone has an idea please clarify.

Thanks
Nanduri


SCJP 1.6 (94%)
http://faq.javaranch.com/java/JspFaq
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
Hi,

1) Class.newInstance() [reflection API] is another method. ex: Object o = Class.forName("someclass").newInstance();

2) Object.clone() creates instances.

3) Autoboxing of primitive types creates instances. ex: Integer n = 25000;

4) ObjectInputStream.readObject() also creates instances, but its implementation probably involves reflection itself.
Jeevan Reddy
Ranch Hand

Joined: Nov 10, 2009
Posts: 142
Thanks Karthik, that is really helpful
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Please search for this; such a question comes up frequently. Particularly in the "beginning Java" forum, where I shall take the liberty of moving this thread (I hope you don't mind, Henry).

Two more examples:
  • A String like "Campbell" is an object in its own right.
  • An array initialiser, eg int[] numbers = {1, 2, 3};
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ways of instantiation
     
    Similar Threads
    Instantiation
    Advantage or Disadvantage?
    constructor + private access
    What type of call is Class instantiation in a Sequence Diagram?
    object instantiation VS initialization