• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make an instance of a class?

 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Ok you must be wondering if am crazy cause am asking how to make an istance of a class. Ok let me first point out I know how to make an instance of a class .
However I have an unsual situation.
Consider the short code below


now the code does what it is suppose to do. However I would like to get rid of the lines
Link link1 = new Link() ;
Link link2 = new Link() ;
because the user has to put in these lines. Suppose I had a thousand for loops! Then I would have to write :
Link link1 = new Link() ;
Link link2 = new Link() ;
Link link3 = new Link() ;
Link link4 = new Link() ;
and so on..
So what am trying to do is that for each for loop the program creates its own name for a Link instance. The code I have given you is a very simplified version of a program am working on.
You see in my program I have a class that is required to make an instance of a paricular class at random times in the program. Hence the user cannot write the names of each instance becasue the does not know how many instances the program will need as the creation of an instance is random. So i need the program itself to make a new class everytime it needs a new instance. And each instance had to have its own unique name.
Can anyone give me some ideas or code please.
Thanks a mllion.
Yours repesctufully Avin Sinanan
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it need a unique name? Why not just use an array?
How do you know how many objects you need?

[ March 09, 2002: Message edited by: Rob Ross ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
What about an array of Link classes?
Since, you don't know how many objects you're going to be using, an array of objects, a Vector, an ArrayList, or some other Collections class would seem the perfect thing to use.
The array of Link objects route:
Link[] myLinks = new Link[0];
And then each time a new Link instance is desired, recreate myLinks as a new Link array one size longer, copy the old array into the new array and tack on the new array at the end.
Then your for loops could always be of the form:

Of course, your Link objects would have to know how long they were (have a property length or a method length() that counted its length and returned that integer value).
Good Luck,
-Dirk Schreckmann
[ March 09, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got two answers at once, though I'm not sure I'm correctly understanding what you're trying to do (I think I answered a question that you didn't ask). It looks like Rob got one part of what you are looking for, but what about this part:



Were you looking for a way to handle the varying values you pass to the Link object's method method1(int)? Perhaps not.
Good Luck,
-Dirk Schreckmann
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!