• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

naming objects dynamically

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
howdy howdy fellow java geeks...

im an extreme N00B when it comes to java, but im decent with other SIMILAR languages...

heres my problem:

i need to basically 'name' objects 'dynamically'...but for some reason, it just isnt working...here's my code...can anyone tell me why this wont work?



the "employeeCount" variable is an auto incrementing variable, that i was HOPING to use to create a unique instance name for the object...can this be done? or are all the instantiated objects stored in an array and only accessable through that array?

thank you so much in advance!

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create an Employee object and want to keep a reference, you'll have to assign that reference to a variable that is declared as Employee or some superclass or interface. So you could try any of these:

What's common to each of those is that we putting a reference to the new object into a variable declared to handle it. Now, you want to keep a bunch of employee objects for various employees and be able to get them back later. After getting our reference we could call methods something like this:

Take a look at the JavaDoc for Map. Maps let us store object instances with some kind of key. I used empId as the key. I can put an object in for id "123" and get it back later. The syntax you showed - which isn't quite Java syntax - was probably trying to imply something much like that.

That threw a lot of new stuff out there all at once. Any of it makes sense? Or not? If you have a specific programming task, take a shot at it, post some code here and we'll see if we can guide you along. Keep having fun!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Java is a compiled language, all variable names must be resolved at compile-time. This may seem strange if you have experience with an interpreted language like Perl where you can build the variable name from the values of other variables. However, Java provides enough other tools that this usually isn't a deficiency. For instance, since you are using a counter in attempting to create the "dynamic variable name", this suggests that an array would be very suitable. Stan's first example above includes an array, but if you are unfamiliar with arrays, then you should google for the syntax and other details. If you need a more sophisticated system to look up a value, rather than just by an integer index, then you should look at the Map class, as Stan suggested. However, I suspect this may be something to just remember for another day as it seems to me that an array will do just fine in this situation.

Keep Coding!

Layne
 
kasey mccurdy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off...

YOU TWO ARE THE SH1T.

thank you so much for helping a noob such as myself, and Layne, you were right, i am very used to Actionsript/ASP/PHP where i can create references to objects and whatnot using variable names at runtime...i understand now why i cant do that!

and Stan, thanks for the array solution, worked like a charm...i was wondering if java didnt automatically put all objects into some sort of accessable array, but putting them in my OWN array is much smarter...just never knew how to do this!

thank you so much! PROBLEM RESOLVED!
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kasey mccurdy:
First off...

YOU TWO ARE THE SH1T.



Isn't that usually an insult???
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kids today and their hip lingo. Ya never know. Shiznit is a great accolade, originating from "that's good shiznit!"
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. Maybe I'm getting old...
If we said things like that in class we'd be sent to the head master and given punishment in the form of cleaning the bathrooms or sweeping the school yard after hours for several days...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
Kids today and their hip lingo. Ya never know. Shiznit is a great accolade, originating from "that's good shiznit!"


Well, I'm not exactly a kid any more. In fact, I've taken a LONG time to finish my Bachelor's degree. Apparently I'm still young enough to understand the lingo, though!

Layne
[ February 06, 2005: Message edited by: Layne Lund ]
 
kasey mccurdy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im actually 23..so dont know if that classifies me as a kid...
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm nearly twice that, so you're not too old for me to muss your hair when you spout that modern slang I see you started this at 5 posts. If we missed the first one, welcome to the ranch! Keep coding, keep having fun.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic