Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Sierra
  • 1 Post By Diargg

Thread: how do you create new classes dynamically without hardcoding names

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default how do you create new classes dynamically without hardcoding names

    ok so imagine I'm inputing information for a customer class... account number, name, balance etc.
    I want to do this for an indefinate amount of customers.

    logically, I want the accountnumber to become the name of the instance being created. (I assume)

    I cant write CustomerAc accountNumber = new CustomerAc because java thinks I'm creating a duplicate variable

    whats the solution here? thank you

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: how do you create new classes dynamically without hardcoding names

    It is not logical to me... ;)
    The solution is to use an array or a list to store your CustomerAc objects:

    Java Code:
    ArrayList<CustomerAc> myList = new ArrayList<CustomerAc>();
    myList.add(new CustomerAc());
    ArrayList (Java Platform SE 7 )
    EscSequenceAlpha likes this.
    I like likes!

  3. #3
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: how do you create new classes dynamically without hardcoding names

    A HashMap may serve you well.

    HashMap<Integer?, CustomerAc> customers = new HashMap<accountNumber, CustomerAc>();
    customers.put(accountNumber, new CusomerAc(data));
    to retrieve, customers.get(accountNumber) will return the associated CustomerAc

    But Sierra's use of a list makes more sense, if accountNumber is stored inside the CustomerAc object.
    EscSequenceAlpha likes this.

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: how do you create new classes dynamically without hardcoding names

    Quote Originally Posted by Sierra View Post
    It is not logical to me... ;)
    The solution is to use an array or a list to store your CustomerAc objects:

    Java Code:
    ArrayList<CustomerAc> myList = new ArrayList<CustomerAc>();
    myList.add(new CustomerAc());
    ArrayList (Java Platform SE 7 )
    Sorry, I havent done dynamic arrays yet so I didnt think of this... I thought I could just use a sentinel value while loop to create instances using the ac number as the name of each instance

    ok, I'll come back to this application once I properly look into these mysterious arraylists

    thank you

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: how do you create new classes dynamically without hardcoding names

    If you know before how many you will create you can also use a normal array:
    CustomerAc[] myArray = new CustomerAc[27];
    maArray[0] = new CustomerAc();
    maArray[1] = new CustomerAc();
    ...

    However I think it is best if you do it with the ArrayList - it is not really complicated once you got the basics.
    I like likes!

Similar Threads

  1. Replies: 1
    Last Post: 04-14-2012, 01:54 PM
  2. create and array with variable names
    By jmrire in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 09:56 PM
  3. Dynamically interpreting/importing code/classes
    By ~ in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2011, 05:06 AM
  4. Replies: 1
    Last Post: 07-06-2010, 05:50 PM
  5. [SOLVED] Can variable names be dynamically created?
    By CJSLMAN in forum New To Java
    Replies: 4
    Last Post: 01-03-2009, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •