Results 1 to 5 of 5
- 04-30-2012, 05:32 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
- 04-30-2012, 06:15 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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:
ArrayList (Java Platform SE 7 )Java Code:ArrayList<CustomerAc> myList = new ArrayList<CustomerAc>(); myList.add(new CustomerAc());
I like likes!.gif)
- 04-30-2012, 06:22 PM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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.
- 04-30-2012, 08:11 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: how do you create new classes dynamically without hardcoding names
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
- 04-30-2012, 09:04 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
Similar Threads
-
Preventing dynamically loaded classes from using reflection
By kjkrum in forum Advanced JavaReplies: 1Last Post: 04-14-2012, 12:54 PM -
create and array with variable names
By jmrire in forum New To JavaReplies: 1Last Post: 02-19-2012, 08:56 PM -
Dynamically interpreting/importing code/classes
By ~ in forum Advanced JavaReplies: 1Last Post: 04-27-2011, 04:06 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
[SOLVED] Can variable names be dynamically created?
By CJSLMAN in forum New To JavaReplies: 4Last Post: 01-03-2009, 01:06 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks