File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to create multiple objects dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create multiple objects dynamically" Watch "How to create multiple objects dynamically" New topic
Author

How to create multiple objects dynamically

Ananth Mahadevan
Greenhorn

Joined: Dec 28, 2011
Posts: 3
Hi,

I have a class like this. Based on a certain value (which can change), I want to create objects. For eg.: if the value is 3, I want to create 3 objects for the below class. If the value is 10, I want to create 10 objects. The value is obtained from the first line of a file.

Please let me know how I can do this?



I want to get something like this, but dynamically:


Thanks,
Ananth.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I assume your question is not actually how to create the objects, you seem to know how to do that. It appears you don't know where to put them once you have created them. Well, you should put your created objects into an array, or a List.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html might help
Ananth Mahadevan
Greenhorn

Joined: Dec 28, 2011
Posts: 3
Will it be something like this?

Assuming value to be 5.
for (i = value; i > 0; i--) {
Inputs inputs = new Inputs();
}

But how can I get 5 distinct objects?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

Already answered:
Paul Clapham wrote:you should put your created objects into an array, or a List.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ananth Mahadevan
Greenhorn

Joined: Dec 28, 2011
Posts: 3
Ok got it. Thank you for the quick response.
 
 
subject: How to create multiple objects dynamically