Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ways of instantiation

 
Jeevan Reddy
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karthik Shiraly
Bartender
Pie
Posts: 1058
24
Android C++ Java Linux PHP Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Karthik, that is really helpful
 
Campbell Ritchie
Sheriff
Pie
Posts: 47259
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic