Results 1 to 6 of 6

Thread: Instantiate different objects of same class

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Instantiate different objects of same class

    Hello,
    I want to do something like this
    If the user types "3", I would like 3 times instantiate the same class
    but with a different object name.
    If the user types "5", I would like 5 times instantiate the same class
    but with a different object name.
    That is in short I want to instantiate different objects of the same class as the users enters the number of objects to be created. If you know who this can be done then please let me know. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Instantiate different objects of same class

    Hello,
    I think you can use the ArrayList to do the same, that is what you need in your class.
    Code:
    ArrayList l = <mobj>new ArrayList();
    for(int i =0; i <args.length; i + +)
    {
    	l(i) = new MyObject();
    }
    Hope this method helps you.

  3. #3
    Join Date
    Nov 2009
    Posts
    356

    Re: Instantiate different objects of same class

    Hello,
    In fact the problem you meet is a classic java. As you had suggested the first speaker, you could use a data structure more efficient that a table. operations of additions and deletion of objects are made of more elegant way. If you need more information on this then you can visit the sun's official site for more detailed information on the same.

  4. #4
    Join Date
    Nov 2008
    Posts
    240

    Re: Instantiate different objects of same class

    Hello,
    Though I am new to the java programming language, I have a code which may interest you.
    Here is the code
    Code:
     
    if(args.equals("3"))
    {
    	byte[] t0 = new byte[sz];
    	byte[] t1 = new byte[sz];
    	byte[] t3 = new byte[sz];
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    202

    Re: Instantiate different objects of same class

    Hello,
    Yes that is correct, but I want to do something like this in my program that is the source code

    Code:
    for(int x =0; x <args.length; x + +)
    {
    	byte[] + x = new byte[SIZE];
    }
    I hope you are understanding my needs and you will provide the necessary.

  6. #6
    Join Date
    Nov 2009
    Posts
    347

    Re: Instantiate different objects of same class

    Hello,
    The factory would not solve the problem. That's what I suggest if you really want to associate names to your objects:
    Here is the part of code for the same
    Code:
    HashMap h = <String, Objet> new HashMap <String, Objet>();
    for(int x =0; I <Integer.parseInt(args[0]); I + +){
        h.could("Purpose" + (x + 1), new Subject());
    }

Similar Threads

  1. How Objects of class are created in C++?
    By Alibamu in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 06:24 AM
  2. Instantiate object of a class
    By New ID in forum Software Development
    Replies: 5
    Last Post: 08-03-2010, 10:03 AM
  3. Accessing Objects Outside Of Class
    By Level8 in forum Software Development
    Replies: 4
    Last Post: 22-02-2010, 08:22 PM
  4. How to sort vector objects by class member variables in c++?
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:15 PM
  5. Replies: 3
    Last Post: 20-08-2009, 03:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,252,875.44664 seconds with 16 queries